Change text on / off Android toggle button

I just changed the background of ToggleButton , and now I want to change the text ON / OFF that goes with it. What is the easiest way to do this?

+72
android togglebutton
Dec 29 '11 at 20:56
source share
7 answers

To install text from code, you can use the following:

 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 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.

+183
Dec 29 '11 at 9:01
source share

In the example you are referring to, they change it to Day / Night using android:textOn and android:textOff

+16
Dec 29 2018-11-21T00:
source share

Define 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" /> 
+9
Dec 20 '14 at 9:18
source share

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

 <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"/> 
+2
Dec 09 '15 at 6:57
source share

Yes you can change the on / off button

  android:textOn="Light ON" android:textOff="Light OFF" 

See details

http://androidcoding.in/2016/09/11/android-tutorial-toggle-button

+2
Sep 11 '16 at 12:47
source share

You can do this in two ways:

Option 1: setting its xml attributes

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

Option 2: software

Set the onClick: methodNameHere attribute (I have 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, I hope it works for you too

+1
Jan 20 '19 at 17:53
source share

In some cases, you need to force the view to work.

 toggleButton.setTextOff(textOff); toggleButton.requestLayout(); toggleButton.setTextOn(textOn); toggleButton.requestLayout(); 
0
Apr 10 '19 at 6:44
source share



All Articles