Android Toggle Button can be used to display checked/unchecked (On/Off) state on the button. It is beneficial if user have to change the setting between two states. By default, the android ToggleButton will be in OFF (Unchecked) state. We can change the default state of ToggleButton by using android:checked attribute.

As AppCompatToggleButton is advance version of ToggleButton we recommend to user AppCompatToggleButton

public class AppCompatToggleButton extends ToggleButton
↳ android.view.View
  ↳ android.widget.TextView
    ↳ android.widget.Button
      ↳ android.widget.CompoundButton
        ↳ android.widget.ToggleButton
          ↳ androidx.appcompat.widget.AppCompatToggleButton

In case, if we want to change the state of ToggleButton to ON (Checked), then we need to set android:checked = "true" in our XML layout file.

We Recommend to use new UI components i.e. androidx components


Xml Attrib Description
android:disabledAlpha The alpha to apply to the indicator when disabled.
android:textOff The text for the button when it is not checked.
android:textOn The text for the button when it is checked.


Method Description
getTextOff() Returns the text when button is not in the checked state.
getTextOn() Returns the text for when button is in the checked state.
setChecked(boolean checked) Changes the checked state of this button.

Create AppCompatToggleButton layout xml file as follow

    android:textOff="Sound Enable"
    android:textOn="Sound Disable"
    android:layout_height="wrap_content" />
final AppCompatToggleButton toggleButton = findViewById(;
toggleButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        Toast.makeText(ToggleButtonActivity.this, toggleButton.getText(), Toast.LENGTH_SHORT).show();

Create AppCompatToggleButton programatically as follow:

AppCompatToggleButton toggleButton = new AppCompatToggleButton(this);
LinearLayoutCompat.LayoutParams params = new LinearLayoutCompat.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

toggleButton.setTextOff("Sound Disable");
toggleButton.setTextOn("Sound Enable");

// you can set checked either true or false accordingly