Two checkboxes, can only select one

15,592

Solution 1

Android developers: http://developer.android.com/reference/android/widget/CheckBox.html

Answer to a similar question: https://stackoverflow.com/a/13133373/2315997

Solution 2

Its very simple, Try this code

public onCreate(Bundle savedInstanceState)
{
    cbx_hindi.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked)
            {
                cbx_english.setChecked(false);
            }
        }
    });
    cbx_english.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked)
            {
                cbx_hindi.setChecked(false);
            }
        }
    });
}
Share:
15,592
scibor
Author by

scibor

please delete me

Updated on June 15, 2022

Comments

  • scibor
    scibor almost 2 years

    I'm working on a login screen where basically you can selected whether you want a 4 digit PIN access or a swipe method security; the way I want this to look is that there are two checkboxes in the layout, initially they're both unchecked. When you select one, it gets checked, but then if you try to select the other one, it unchecks the first one automatically. I have two checkboxes defined in the layout, but I'm a little confused by what I'd have to reference to force the unchecking.

    public void onCheckBoxClicked(View view){
        boolean checked = ((CheckBox)view).isChecked();
        switch(view.getId()){
            case R.id.setupCheckBox1:
                if(checked){
    
                }
    

    Basically, I know that something has to go into that if{} statement that unchecks setupCheckBox2 automatically, but I'm just unsure of what has to be made false. Any help appreciated!

    Also, I realize this is kind of a one line answer, if anyone has any good references to checkboxes in Android, I wouldn't mind reading up on that either, I'm kind of new to this stuff.