Using Toggle Button and Switch in Android App
This topic is a part of My Complete Andorid Course
ToggleButton toggleButton;
Switch aSwitch;
toggleButton = findViewById(R.id.toggleButton);
aSwitch = findViewById(R.id.switch1);
toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
Toast.makeText(getApplicationContext(), "Checked", Toast.LENGTH_SHORT).show();
else
Toast.makeText(getApplicationContext(), "Unchecked", Toast.LENGTH_SHORT).show();
}
});
aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
Toast.makeText(getApplicationContext(), "Switch Checked", Toast.LENGTH_SHORT).show();
else
Toast.makeText(getApplicationContext(), "Switch Unchecked", Toast.LENGTH_SHORT).show();
}
});