user3128007
user3128007

Reputation: 37

Android TextView setText

Okay, so I have this code:

<?xml version="1.0" encoding="utf-8?">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center"
android:orientation="vertical">

<TextView
    android:id="@+id/Text"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_gravity="center"
    android:text="Text"/>
</LinearLayout>

(Main Activity) below:

TextView text = (TextView) findViewById(R.id.Text);
text.setText("Text Replacement!");

This doesn't replace the text, and I do have the class, and main methods. onCreate etc.

It seems that the XML file is the only one it will take a value from, if there's no android:text it shows nothing.

I'm basically trying to just change the text via the .setText function, what am I doing wrong?

Thanks

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView text = (TextView) findViewById(R.id.Text);
text.setText("Text Replacement");
  }
}


Layouts for files: /storage/emulated/0/AppProjects/New/res/layout/main.xml
                   /storage/emulated/0/AppProjects/New/gen/com/mycompany/New/R.java
                   /storage/emulated/0/AppProjects/New/src/com/mycompany/new/MainActivity.java

Upvotes: 0

Views: 2373

Answers (7)

Melquiades
Melquiades

Reputation: 8598

Move your findViewById after setContentView(), and change your variable names to follow Java convention:

public class MainActivity extends Activity {

    @Override
    public void onCreate(savedInstanceState){
        super.onCreate(Bundle savedInstanceState);
        setContentView(R.layout.main);

        //this line should be called after you inflate your view with setContentView
        //so move it here, instead in MainActivity body
        TextView text = (TextView) findViewById(R.id.Text);
        text.setText("Text Replacement");
    }
}

Also, there's a typo in your xml layout. Change this line:

<?xml version="1.0" encoding="utf-8?">

to

<?xml version="1.0" encoding="utf-8"?>

Upvotes: 2

Sush
Sush

Reputation: 3874

You should innialize your view and set proper properties in andriod after setContentView method is been called.

So this below code should come after setcontent view

    TextView text = (TextView) findViewById(R.id.Text);
    text.setText("Text Replacement);

Upvotes: 1

Raghunandan
Raghunandan

Reputation: 133580

Move the below to onCreate

TextView Text = (TextView) findViewById(R.id.Text);

Like

 @Override
 public void onCreate(Bundle savedInstanceState){
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 TextView tv = (TextView) findViewById(R.id.Text);
 tv.setText("Text Replacement); 

findViewById looks for a view with the id in the current inflated layout. So first you need to set the layout to the activity and then initialize views.

Also consider renaming Text to something more appropriate and meaningfull.

Edit:

Make sure you have the textview with id Text in main.xml. Build and clean your project.

Upvotes: 1

Chintan Soni
Chintan Soni

Reputation: 25287

You cannot find your views before setContentView() method. Move your lines after setContentView():

TextView Text = (TextView) findViewById(R.id.Text);
Text.setText("Text Replacement);

Also, please follow java naming conventions, as obect names never start with Caps but class names do..

Upvotes: 1

Vivek Warde
Vivek Warde

Reputation: 1514

Ur variable is declared as TextView Text

Instead u should use text because in Java Anything starting with capital letter is treated as a class ,it is a bad programming practice ! The variables should be in lowercase(hungerian notation) !

Upvotes: 0

Quentin S.
Quentin S.

Reputation: 1469

First, consider using "wrap_content" or "match_parent" attributes instead of "200dp" for example.

<TextView
    android:id="@+id/Text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="Text"/>

Then, please rename your "Text" variable into "text". Java or Android may be thinking that Text is not your variable but a class:

TextView text = (TextView) findViewById(R.id.Text);
text.setText("Text Replacement!");

Upvotes: 0

gilonm
gilonm

Reputation: 1859

Looks like you have two "" before the "Text"

Try deleting one... Should look like this:

android:text="Text"/>

Upvotes: 0

Related Questions