0

In an XML file, a textview was being made, now I want to edit it's text property from the .java source file. I tried referring to its id, but eclipse wont recognize it; "cannot be resolved"

relevant part from XML:

<TextView
    android:id="tb_01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

from the source:

Intent intent = getIntent();
    String text = intent.getStringExtra(text);

    tb_01.setText(text);

also, could someone explain, what this does in an xml, within a editText tag:

android:id="@+id/edit_message"
Senki Sem
  • 131
  • 1
  • 9

3 Answers3

4

You need to provide id like...

<TextView
    android:id="@+id/tb_01"
                ^^^^^^^^^^
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

You can find difference between @id and @+id/ at here.

From java code reference it using this way..

Intent intent = getIntent();
String text = intent.getStringExtra(text);
TextView tb_01= (TextView) findViewById(R.id.tb_01);
tb_01.setText(text);
Community
  • 1
  • 1
Hardik Joshi
  • 9,477
  • 12
  • 61
  • 113
1

Get the Textview and set the text:

TextView tv = (TextView) findViewById(R.id.tb_01);

tv.setText("your text");

The

"@+id/edit_Message"

just creates a new resource id, which can be used to access that element programatically. Like "findViewById()" from above sample.

Hardik Joshi
  • 9,477
  • 12
  • 61
  • 113
nouseforname
  • 720
  • 1
  • 5
  • 17
1

The id should be like this

<TextView
    android:id="@+id/tb_01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

Then you will be able to get your TextView

yOU can get more information about these id's from the following link

Difference between “@id/” and “@+id/” in Android

R.id

Community
  • 1
  • 1
Trikaldarshiii
  • 11,174
  • 16
  • 67
  • 95