Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two checkboxes, can only select one

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.

like image 660
scibor Avatar asked Jun 07 '13 14:06

scibor


2 Answers

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

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

like image 131
jschabs Avatar answered Oct 30 '22 10:10

jschabs


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);
            }
        }
    });
}
like image 36
Mehul Avatar answered Oct 30 '22 11:10

Mehul