how to allow only 1 android toggle button out of 3 to be on at once
Asked Answered
V

5

9

i am using 3 toggle buttons. In my android application i would like that only 1 of these toggle buttons can be selected at once. How would i go about doing this?

Vermiculite answered 8/6, 2011 at 17:22 Comment(1)
You can use a RadioGroup (developer.android.com/reference/android/widget/RadioGroup.html). See this thread. #2380027Tunisia
R
3

You could use radio buttons. If you don't want that, check out this link - it shows you how to listen for changes to the button state. If you find that one of your buttons is changed, change the other 2 to the off state.

Rely answered 8/6, 2011 at 17:48 Comment(0)
B
14

A simple onChangeListener will do:

public class TestProjectActivity extends Activity {

ToggleButton one; 
ToggleButton two;
ToggleButton three;
ToggleButton four;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    one = (ToggleButton) findViewById(R.id.toggleButton1);
    two = (ToggleButton) findViewById(R.id.toggleButton2);
    three = (ToggleButton) findViewById(R.id.toggleButton3);
    four = (ToggleButton) findViewById(R.id.toggleButton4);

    one.setOnCheckedChangeListener(changeChecker);
    two.setOnCheckedChangeListener(changeChecker);
    three.setOnCheckedChangeListener(changeChecker);
    four.setOnCheckedChangeListener(changeChecker);
}

OnCheckedChangeListener changeChecker = new OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked){
            if (buttonView == one) {
                two.setChecked(false);
                three.setChecked(false);
                four.setChecked(false);
            }
            if (buttonView == two) {
                one.setChecked(false);
                three.setChecked(false);
                four.setChecked(false);
            }
            if (buttonView == three) {
                two.setChecked(false);
                one.setChecked(false);
                four.setChecked(false);
            }
            if (buttonView == four) {
                two.setChecked(false);
                three.setChecked(false);
                one.setChecked(false);
            }
        }
    }
};

}

Battledore answered 27/7, 2011 at 11:56 Comment(1)
Why CompoundButton vs. RadioGroup?Participation
D
9

ThePikeman's solution is okay, but depending on how many buttons you have you might want to consider an array that you can iterate over.

For a small number of buttons, Pikeman's code can be simplified to save some typing...

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if (isChecked){
      if (buttonView != one) {
        one.setChecked(false);
      }
      if (buttonView != two) {
        two.setChecked(false);
      }
      if (buttonView != three) {
        three.setChecked(false);
      }
      if (buttonView != four) {
        four.setChecked(false);
      }
    }
  }
Dacca answered 7/9, 2013 at 3:45 Comment(0)
R
3

You could use radio buttons. If you don't want that, check out this link - it shows you how to listen for changes to the button state. If you find that one of your buttons is changed, change the other 2 to the off state.

Rely answered 8/6, 2011 at 17:48 Comment(0)
C
0

I use the following function;

private void setIsChecked(CheckBox checkBox){
      buttonOne.setChecked(false);
      buttonTwo.setChecked(false);
      buttonThree.setChecked(false);
      checkBox.setChecked(true)
}

now all of them are deemed except for the one you choose

ex

setIsChecked(buttonOne);

Now only button one is checked and etc..

Coccidiosis answered 17/2, 2017 at 16:27 Comment(0)
B
0

If you're using MaterialButtonToggleGroup you just need to use this property in your xml:

<com.google.android.material.button.MaterialButtonToggleGroup
   ...
   app:singleSelection="true"
   ...>
Brucine answered 10/3, 2023 at 22:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.