Android widget: How to change the text of a button


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

9/15/2013 6:26:19 PM

Accepted Answer

I was able to change the button's text like this:

import android.widget.RemoteViews;

//grab the layout, then set the text of the Button called
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.my_layout);
remoteViews.setTextViewText(, "Set button text here");
9/15/2013 6:46:36 PM

You can use the setText() method. Example:

import android.widget.Button;

Button p1_button = (Button)findViewById(;
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.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow