Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

trigger CheckedChanged programmatically for toggleButton (CompoundButton)

How can I trigger programmatically the following method : (without clicking/swiping the on/off toggle switch)

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
  //randomness
}

I figured an alternative way would be, calling

switch1.setChecked(true);

Then doing a manual check.

like image 908
tony9099 Avatar asked Jun 09 '26 03:06

tony9099


1 Answers

There actually is a way of calling it if you refer to the class methods ;)

ToggleButton t = (ToggleButton) findViewById(R.id.myToggle); t.performClick();

See http://developer.android.com/reference/android/widget/CompoundButton.html#performClick()

like image 180
Mavelo Avatar answered Jun 10 '26 18:06

Mavelo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!