Reputation: 279
I need to show "You are sure?" dialog when user want to change toggle position to off.
I create a check function and assigned a setOnClickListener function to the switch
private fun checkSwitchStatus() {
if (!reminderSwitch.isChecked) {
reminderSwitch.isChecked = true
showCancelPopup()
} else {
reminderSwitch.isChecked = true
}
}
Now the switch does not turn off visually, but the setOnCheckedChangeListener function is still processed and some action happens..
override fun onCheckedChanged(buttonView: CompoundButton?, isChecked: Boolean) {
if (!isChecked) {
showCancelPopup()
} else {
//*some action*
}
}
How to do some action before switch change its state, and be able to undo state change?
Upvotes: 2
Views: 804
Reputation: 591
You can setClickable(false)
on your Switch, then listen for the onClick
event within the Switch's parent and toggle it programmatically. The switch will still appear to be enabled but the swipe animation won't happen and its state won't change.
Since you want to reaffirm the user's choice, show a dialog in onClick
and have your switch's state be dependent on whether a 'yes' was tapped in on the dialog.
// if yes was tapped on the Dialog
switchInternet.setChecked(true);
} else {
switchInternet.setChecked(false);
}
Upvotes: 1