Issue
Hello Guys can someone tell me how i can make a few toggle switches in Java? Specifically two out of three switches that toggle off when one is turned on?
Solution
Lets say you have swt1
and swt2
.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
public class MainActivity extends AppCompatActivity {
private Switch swt1;
private Switch swt2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
swt1 = (Switch)findViewById(R.id.swt1);
swt2 = (Switch)findViewById(R.id.swt2);
swt1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(b == true)
swt2.setChecked(false);
else
swt2.setChecked(true);
}
});
swt2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(b == true)
swt1.setChecked(false);
else
swt1.setChecked(true);
}
});
}
}
You can add as many switches as you want. Just use switch.setChecked(true)
to toggle a switch on, and switch.setChecked(false)
to toggle the switch off.
Be sure to ask any questions!
Answered By - Eddie
Answer Checked By - Cary Denson (JavaFixing Admin)