How to change the text of a Button widget in Android?
Asked Answered
B

7

45

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

Bromine answered 3/10, 2010 at 20:37 Comment(3)
((android.widget.Button)findViewById(R.id.epic_button)).setText("mytitle");Monkish
findViewById does not work for widgets! This method not supported in widgets.Couscous
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");Vitrify
B
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");
Bromine answered 6/10, 2010 at 8:27 Comment(4)
how do I get the text of the button.Blob
Thanks, this saved me some time!You also need to add the ns.notify(notificationID, notification);Cassondra
I'd also like to know, how to set a button's text inside of a widget. using remoteViews.setTextViewText(R.id.MyButton, "sometext") doesn't work for meDrain
The problem is, when I use the onReceive() method (where it used for button event listeners in widget), either the RemoteView is null or even if I set on the field after instantiate via onUpdate().Incantatory
M
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.

Marten answered 3/10, 2010 at 20:41 Comment(6)
But on a widget findViewById doesn't existBromine
@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: developer.android.com/reference/android/widget/…Marten
@Skatephone: You shouldn't call findViewById on a Widget, but on an Activity or a View.Amias
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, "-");Bromine
This solution worked for me, for finding other "widgets" (Views) from the method called at onClick for a button. Thanks.Hamster
My application will stop unexpectedly but it doesn't provide me nay error message...Bookcraft
B
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");
Bromine answered 6/10, 2010 at 8:27 Comment(4)
how do I get the text of the button.Blob
Thanks, this saved me some time!You also need to add the ns.notify(notificationID, notification);Cassondra
I'd also like to know, how to set a button's text inside of a widget. using remoteViews.setTextViewText(R.id.MyButton, "sometext") doesn't work for meDrain
The problem is, when I use the onReceive() method (where it used for button event listeners in widget), either the RemoteView is null or even if I set on the field after instantiate via onUpdate().Incantatory
K
4

This is very easy

Button btn = (Button) findViewById(R.id.btn);
btn.setText("MyText");
Kowatch answered 11/6, 2017 at 5:57 Comment(0)
D
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!

Declinatory answered 25/9, 2013 at 14:51 Comment(0)
C
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); 
Cutis answered 2/8, 2016 at 21:2 Comment(0)
U
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

Unweave answered 25/6, 2015 at 11:26 Comment(0)
M
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"/>
Murraymurre answered 10/8, 2018 at 12:34 Comment(1)
Are you sure about this? Your answer does not contain any code?Flinders

© 2022 - 2024 — McMap. All rights reserved.