ToggleButton: attributes (textOff, textOn), event methods (getTextOff(), getTextOn(), setChecked())

A ToggleButton is a button with two states, on and off, showing textOn or textOff for each, and you read its state with isChecked(), change it with setChecked(true/false), and read its labels with getTextOn()/getTextOff().

8 min read · 9 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

A setting that remembers on or off

FestConnect wants a 'Notifications' control: tap once to turn reminders on, tap again to turn them off, with the label changing to show which state it is in. A plain Button cannot remember a state; it just fires and forgets.

The ToggleButton is built for exactly this: it holds an on/off state, shows a different label for each, and lets your code read and set that state. It also introduces the 'checked' idea that CheckBox and RadioButton reuse.

Theory

A light switch, not a doorbell

A doorbell (a plain Button) does something the instant you press it, then forgets. A light switch (a ToggleButton) stays where you left it: up is on, down is off, and you can see its position at a glance. The switch remembers a state between presses. That memory of 'which state am I in' is the whole difference.

Theory

Labels, state, and reacting

  • Labels: android:textOn (default 'ON') and android:textOff (default 'OFF') set the text for each state.
  • Read state: toggle.isChecked() returns true (on) or false (off).
  • Set state: toggle.setChecked(true) forces it on.
  • Read labels: getTextOn() / getTextOff().
  • React: toggle.setOnCheckedChangeListener((btn, isChecked) -> { ... }); runs when it flips.

(A Switch is the same idea with a sliding look.)

Practical

FestConnect notifications toggle

// XML: <ToggleButton android:id="@+id/notify"
//        android:textOn="Notifications On"
//        android:textOff="Notifications Off" />

ToggleButton notify = findViewById(R.id.notify);

notify.setOnCheckedChangeListener((btn, isChecked) -> {
    if (isChecked) {
        Toast.makeText(this, "Reminders on", Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(this, "Reminders off", Toast.LENGTH_SHORT).show();
    }
});

// read or force the state anywhere:
boolean on = notify.isChecked();
notify.setChecked(true);   // turn it on from code

Think first

isChecked or setChecked?

You want to (a) find out whether notifications are currently on, and (b) turn them on by default when the screen loads. Which method does each, and what does each return or take?

Show the answer

(a) To find out the current state, call isChecked(), which returns a boolean (true = on). (b) To turn it on from code, call setChecked(true), which takes a boolean and sets the state (no return). The pattern is universal for checkable widgets: isChecked() reads, setChecked(...) writes. You will use the exact same pair on CheckBox and RadioButton in the next unit.

Quiz

What do android:textOn and android:textOff control on a ToggleButton, and what are their defaults?

  1. The label shown in each state; defaults are 'ON' and 'OFF'
  2. The colours of the button in each state
  3. The sound played when toggled
  4. Whether the button is enabled or disabled
Show the answer

The label shown in each state; defaults are 'ON' and 'OFF'

textOn and textOff set the text label displayed for the on and off states, defaulting to 'ON' and 'OFF' respectively (A). They do not set colours (B) or sounds (C), and they have nothing to do with enabling/disabling the widget (D). Setting them (e.g. 'Notifications On'/'Notifications Off') makes the toggle self-explanatory, which is good UI on a settings screen.

Watch out

ToggleButton traps

1. Expecting a plain Button to remember state: it does not; use a ToggleButton (or Switch/CheckBox) for on/off.

2. Confusing the label with the state: getTextOn() returns the label; isChecked() returns the boolean state. Do not test the text to know the state.

3. Reading state in the wrong place: to react the moment it flips, use setOnCheckedChangeListener, not a one-time read in onCreate.

Theory

The checked model scales up

isChecked()/setChecked() and the on/off idea are not unique to ToggleButton. Unit 5 opens with the CheckBox, which uses the identical checked model to let a student pick several events, then the RadioButton, which groups checkables so only one can be on. You already know the core method pair; the next widgets just change how many can be checked at once.

Summary

Key takeaways

  • A ToggleButton holds a two-state on/off value and shows a label per state.
  • android:textOn / android:textOff set the labels (defaults 'ON' / 'OFF').
  • isChecked() reads the state (boolean); setChecked(true/false) sets it.
  • getTextOn()/getTextOff() read the labels; setOnCheckedChangeListener reacts when it flips.
  • A plain Button cannot remember state; a Switch is a slider-styled ToggleButton.
  • Memory hook: a light switch (stays put), not a doorbell (fires and forgets).

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Android Widgets (UI)

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

ToggleButton: attributes (textOff, textOn), event methods (getTextOff(), getTextOn(), setChecked()) · Mobile Application Development - 1 (option B) · Gri-Learn