Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the On/Off text of a toggle button Android

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?

like image 807
tyler Avatar asked Dec 29 '11 20:12

tyler


People also ask

How do I toggle an android button?

Since the Android 4.0 version (API level 14), it has another type of toggle button called switch which provides user slider control. Programmatically, isChecked() method is used to check the current state of the toggle button. This method returns a boolean value.

Which control can be used for on off state for a button?

Android Toggle Button is used to display on and off state on a button. Switch is another type of toggle button that's predominantly used since Android 4.0. Android Switch provides a slider control. Both ToggleButton and Switch are subclasses of CompoundButton class.


1 Answers

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

like image 144
rfsk2010 Avatar answered Oct 06 '22 01:10

rfsk2010