88

I just changed the background of a ToggleButton, and now I'm looking to change the ON/OFF text that comes up with it. What is the easiest way to do this?

Community
  • 1
  • 1
Tyler
  • 19,113
  • 19
  • 94
  • 151

6 Answers6

215

You can use the following to set the text from the code:

toggleButton.setText(textOff);
// Sets the text for when the button is first created.

toggleButton.setTextOff(textOff);
// Sets the text for when the button is not in the checked state.

toggleButton.setTextOn(textOn);
// Sets the text for when the button is in the checked state.

To set the text using xml, use the following:

android:textOff="The text for the button when it is not checked."
android:textOn="The text for the button when it is checked." 

This information is from here

Ujjwal Singh
  • 4,908
  • 4
  • 37
  • 54
rfsk2010
  • 8,571
  • 4
  • 32
  • 46
  • 4
    however it doesn't work on e.i. samsung and htc phones – Serafins Sep 21 '14 at 21:20
  • 2
    Serafins, that's not right. It does work on Samsung and HTC phones. – interrupt Sep 23 '14 at 03:09
  • 4
    When updating the on and off text programatically the button doesn't redraw itself with the new text. You can force a redraw by calling setChecked(toggleButton.isChecked). Sounds ridiculous but its a hack to force redrawing. See [this stackoverflow answer](http://stackoverflow.com/a/3792554/2590478). – MidasLefko Apr 29 '15 at 15:21
  • It appears you no longer need toggleButton.setTextOff(textOff); and toggleButton.setTextOn(textOn);. The text for each toggled state will change by merely including the relevant xml characteristics. – Martin Erlic Dec 09 '15 at 06:56
  • 1
    Yes it doesn't work with `android.support.v7.widget.SwitchCompat` on some OEMs I have checked that! – sud007 Sep 27 '16 at 07:08
  • You've call `toggleButton.setSelected()` after setting the ON/OFF text from code to force invalidate the button. – NullPointer Mar 09 '17 at 23:28
  • Nothing of the mentioned works for Nexus for either Switch and SwitchCompat. Only set in xml or use custom drawable – Leo DroidCoder Apr 24 '18 at 12:22
16

In the example you link to, they are changing it to Day/Night by using android:textOn and android:textOff

Mark B
  • 183,023
  • 24
  • 297
  • 295
  • I actually linked to a different question I had never seen before. Thanks for pointing out the answer in my own question. – Tyler Dec 29 '11 at 21:19
13

Set the XML as:

<ToggleButton
    android:id="@+id/flashlightButton"
    style="@style/Button"
    android:layout_above="@+id/buttonStrobeLight"
    android:layout_marginBottom="20dp"
    android:onClick="onToggleClicked"
    android:text="ToggleButton"
    android:textOn="Light ON"
    android:textOff="Light OFF" />
Jacob Holloway
  • 887
  • 8
  • 24
Thiago
  • 12,778
  • 14
  • 93
  • 110
3

In some cases, you need to force refresh the view in order to make it work.

toggleButton.setTextOff(textOff);
toggleButton.requestLayout();

toggleButton.setTextOn(textOn);
toggleButton.requestLayout();
Saurabh Padwekar
  • 3,888
  • 1
  • 31
  • 37
2

It appears you no longer need toggleButton.setTextOff(textOff); and toggleButton.setTextOn(textOn);. The text for each toggled state will change by merely including the relevant xml characteristics. This will override the default ON/OFF text.

<ToggleButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/toggleText"
    android:textOff="ADD TEXT"
    android:textOn="CLOSE TEXT"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="10dp"
    android:visibility="gone"/>
Martin Erlic
  • 5,467
  • 22
  • 81
  • 153
2

You can do this by 2 options:

Option 1: By setting its xml attributes

 `android:textOff="TEXT OFF"
  android:textOn="TEXT ON"`

Option 2: Programmatically

Set the attribute onClick: methodNameHere (mine is toggleState) Then write this code:

public void toggleState(View view) {
   boolean toggle = ((ToogleButton)view).isChecked();
   if (toggle){
       ((ToogleButton)view).setTextOn("TEXT ON");
   } else {
      ((ToogleButton)view).setTextOff("TEXT OFF");
   }
}

PS: it works for me, hope it works for you too

Pocoyo
  • 21
  • 4