45

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

Ola Ström
  • 4,136
  • 5
  • 22
  • 41
Skatephone
  • 1,165
  • 2
  • 12
  • 26
  • `((android.widget.Button)findViewById(R.id.epic_button)).setText("mytitle");` – Eric Leschinski Sep 15 '13 at 18:53
  • findViewById does not work for widgets! This method not supported in widgets. – coolcool1994 Aug 26 '14 at 07:22
  • 1
    It works. Just be sure to add `import android.widget.View;` at the top of your java file. If you also add `import android.widget.Button;`, it can be shortened to: `((Button)findViewById(R.id.yourButtonName)).setText("New Text");` – Tyler Pantuso Jan 27 '16 at 06:25

7 Answers7

61

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.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
eldarerathis
  • 35,455
  • 10
  • 90
  • 93
  • 7
    But on a widget findViewById doesn't exist – Skatephone Oct 03 '10 at 22:06
  • 2
    @Skatephone: It will be easier to help you if you post a little bit of code, then. That being said, the `RemoteViews` class has a `setTextViewText()` method. I'd suggest taking a look at that: http://developer.android.com/reference/android/widget/RemoteViews.html#setTextViewText%28int,%20java.lang.CharSequence%29 – eldarerathis Oct 03 '10 at 23:40
  • @Skatephone: You shouldn't call findViewById on a Widget, but on an Activity or a View. – benvd Oct 04 '10 at 07:27
  • When i receve of a certain button pressed in the onReceive method i try this: RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main); remoteViews.setTextViewText(R.id.ButtonMeno, "-"); – Skatephone Oct 04 '10 at 09:05
  • This solution worked for me, for finding other "widgets" (Views) from the method called at onClick for a button. Thanks. – Alexander Sep 15 '11 at 13:42
  • My application will stop unexpectedly but it doesn't provide me nay error message... – Alston May 16 '14 at 02:52
23

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 R.id.Counter:
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.my_layout);
remoteViews.setTextViewText(R.id.Counter, "Set button text here");
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Skatephone
  • 1,165
  • 2
  • 12
  • 26
4

This is very easy

Button btn = (Button) findViewById(R.id.btn);
btn.setText("MyText");
Jai
  • 3,211
  • 2
  • 17
  • 26
1

I had a button in my layout.xml that was defined as a View as in:

final View myButton = findViewById(R.id.button1);

I was not able to change the text on it until I also defined it as a button:

final View vButton = findViewById(R.id.button1);
final Button bButton = (Button) findViewById(R.id.button1);

When I needed to change the text, I used the bButton.setText("Some Text"); and when I wanted to alter the view, I used the vButton.

Worked great!

Aryan
  • 136
  • 1
  • 3
  • 17
bob
  • 688
  • 8
  • 15
1

use the exchange using java. setText = "...", for class java there are many more methods for implementation.

    //button fechar
    btnclose.setEnabled(false);
    btnclose.setText("FECHADO");
    View.OnClickListener close = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (btnclose.isClickable()) {
                btnOpen.setEnabled(true);
                btnOpen.setText("ABRIR");
                btnclose.setEnabled(false);
                btnclose.setText("FECHADO");
            } else {
                btnOpen.setEnabled(false);
                btnOpen.setText("ABERTO");
                btnclose.setEnabled(true);
                btnclose.setText("FECHAR");
            }

            Toast.makeText(getActivity(), "FECHADO", Toast.LENGTH_SHORT).show();
        }
    };

    btnclose.setOnClickListener(close); 
0

This may be off topic, but for those who are struggling on how to exactly change also the font of the button text (that was my case and Skatephone's answer helped me) here's how I did it (if you made buttons ind design mode):

First we need to have the button's string name "converted" (it's a foul way to explain, but straightforward) into java from the xml, and so we paste the aforementioned code into our MainActivity.java

IMPORTANT! place the code under the OnCreate method!

import android.widget.RemoteViews;

RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.my_layout);
remoteViews.setTextViewText(R.id.Counter, "Set button text here");

Keep in mind:

my_layout has to be substituted with the xml file where your buttons are

Counter has to be substituted with the id name of your button ("@+id/ButtonName")

if you want to change the button text just insert the text in place of "Set button text here"

here comes the part where you change the font:

Now that you "converted" from xml to java, you can set a Typeface method for TextView. Paste the following code exactly under the previous one just described above

TextView txt = (TextView) findViewById(R.id.text_your_text_view_id);
        Typeface font = Typeface.createFromAsset(getAssets(), "fonts/MyFontName.ttf");
        txt.setTypeface(font);

where in place of text_your_text_view_id you put your button's id name (like as previous code) and in place of MyFontName.ttf you put your desired font

WARNING! This assumes you already put your desired font into the assets/font folder. e.g. assets/fonts/MyFontName.ttf

0

//text button:

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=" text button" />

// color text button:

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="text button" 
        android:textColor="@android:color/color text"/>

// background button

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="text button" 
        android:textColor="@android:color/white"
        android:background="@android:color/ background button"/>

// text size button

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="text button" 
        android:textColor="@android:color/white"
        android:background="@android:color/black"
        android:textSize="text size"/>