Click here to Skip to main content
16,004,727 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Using the following code I am unable to show from MainActivity.java the text value 'Set in Java' on to the emulator. I am able to see the plain emulator only. Hereunder I submit the code for strings.xml, textview and java.

strings.xml:
XML
<resources>

    <string name="app_name">My Application</string>
    <string name="textview">"@string/textview"</string>
    <string name="action_settings">Settings</string>

</resources>

------
 <textview>
        android:id="@+id/main_textview"
        android:text="@string/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"

    />

-----
Java
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {
    TextView mainTextView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 1. Access the TextView defined in layout XML
        // and then set its text
        mainTextView = (TextView) findViewById(R.id.main_textview);
        mainTextView.setText("Set in Java!");

    }
Posted
Updated 16-Jan-15 3:27am
v2
Comments
Richard MacCutchan 16-Jan-15 9:31am    
Which file contains the <textview> resource?
S.Rajendran from Coimbatore 16-Jan-15 9:50am    
activity_main.xml

1 solution

Your XML file is wrong, you have spelled textview in all lower case (textview instead of TextView), and your opening tag has a closing brace (>). It should be:
XML
<TextView
        android:id="@+id/main_textview"
        android:text="@string/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
 
    />
 
Share this answer
 
v3
Comments
Peter Leow 16-Jan-15 10:48am    
5ed!
Richard MacCutchan 16-Jan-15 10:52am    
What did you 5?
Peter Leow 16-Jan-15 11:05am    
Strange. The stars just disappeared when I return to this page even after several tries. But it is shown in my history. Will wait and see if it comes back. Possibly bug.
Richard MacCutchan 16-Jan-15 11:10am    
Well I have one upvote in my rep history but I'm not sure if it was from you or Mr Rajendran; very odd, but thanks anyway.
S.Rajendran from Coimbatore 16-Jan-15 10:58am    
I changed accordingly but not getting displayed.
<textview
android:id="@+id/main_textview"
="" android:text="@string/textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="20dp" android:layout_margintop="20dp"

="">

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900