I have a method like this:
ToggleButton toggle = ((ToggleButton)findViewById(R.id.toggle));
toggle.setTextOn("blah");
toggle.setTextOff("blahblah");
toggle.invalidate(); // doesn't work?
This methods gets called from onOptionsItemSelected
. The toggle button is inside a LinearLayout
which is inside another LinearLayout
.
I'm expecting the text to update as soon as the method is called. Rather the text on the toggle only updates after I manually press it to switch states. What am I missing here, am I using the wrong method? Why doesn't .invalidate
work?
Instead of invalidating, you can update the button like this:
toggle.setChecked(toggle.isChecked());
Update:
It is weird that setTextOff
and setTextOn
don't update the button. However setText
does that. So just set the text for each button states and setText
to update current value:
// Text based on state
ToggleButton toggle = ((ToggleButton)findViewById(R.id.toggle));
toggle.setTextOn("blah");
toggle.setTextOff("blahblah");
CharSequence text = toggle.isChecked() ? toggle.getTextOn() : toggle.getTextOff();
toggle.setText(text);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With