Android widget: How to change the text of a button

Skatephone picture Skatephone · Oct 3, 2010 · Viewed 127.6k times · Source

How can I change the text of an Android button widget within code and not the XML file?

Answer

eldarerathis picture eldarerathis · Oct 3, 2010

You can use the setText() method. Example:

import android.widget.Button;

Button p1_button = (Button)findViewById(R.id.Player1);
p1_button.setText("Some text");

Also, just as a point of reference, Button extends TextView, hence why you can use setText() just like with an ordinary TextView.