drdrdr
drdrdr

Reputation: 2948

Get the text inside a button

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

How do I get the text inside android:text?

Upvotes: 5

Views: 445

Answers (6)

Michael
Michael

Reputation: 54735

TextView button1 = (TextView)findViewById(R.id.button1);
String text = button1.getText().toString();

Upvotes: 3

Pattabi Raman
Pattabi Raman

Reputation: 5854

Button btn = (Button)findViewById(R.id.button1);
String buttontext = btn.getText().toString();

Hope this will help you.

Upvotes: 9

Che Jami
Che Jami

Reputation: 5151

Try to keep all your strings in the strings.xml (rather than hard-coding them everywhere)

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

In the strings.xml:

<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.

Upvotes: 1

mthpvg
mthpvg

Reputation: 3819

Button tv = (Button) findViewById(R.id.button1);
String buttonName = tv.getText().toString();

Edit according to the comment below, thanks :).

Upvotes: 2

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

Upvotes: 2

Yashwanth Kumar
Yashwanth Kumar

Reputation: 29141

you get text by.

Button button = (Button) findViewById(R.id.button1);
String yourText = (String)button.getText();

Upvotes: 3

Related Questions