How to get the text inside android:text? +5 an...">

Get text inside a button

<Button android:id="@+id/button1" android:text="blah blah">

How to get the text inside android:text?

+5
source share
6 answers
Button btn = (Button)findViewById(R.id.button1);
String buttontext = btn.getText().toString();

Hope this helps you.

+9
source

you will get the text.

Button button = (Button) findViewById(R.id.button1);
String yourText = (String)button.getText();
+3
source
TextView button1 = (TextView)findViewById(R.id.button1);
String text = button1.getText().toString();
+3
source
Button tv = (Button) findViewById(R.id.button1);
String buttonName = tv.getText().toString();

Change as per the comment below, thanks :).

+2
source
Button mButton;
mButton=(Button)findViewById(R.id.button1);
String buttontext=  mButton.getText().toString();
+2
source

Try saving all your lines in strings.xml (rather than hard-coded them everywhere)

<Button android:id="@+id/button1" android:text="@string/button1_label">

In the strings.xml file:

<string name="button1_label">blah blah</string>

Then you can easily get the text using:

Context.getString(R.string.button1_label)

You can find out more here.

+1
source

All Articles