I want when click on check box1, then it chekecd and if checkbox2 is checked before , force it to no checked state(for android)

StackOverflow https://stackoverflow.com//questions/21049584

Question

Hello stackoverflow friends. I am new by android and I have a simple question thet bothers me! I have 2 CheckBox(CheckBoxAutomat and CheckBoxManual for example) . I want when click on CheckBoxAutomat, then it chekecd and if CheckBoxManual is checked before , force it to no checked state. And for CheckBoxManual these events must be done. I write this code but it has wrong result for me. How I can change checked status for my chechboxes according explainations? Code:

               CheckBoxAutomat.setOnCheckedChangeListener(new OnCheckedChangeListener() 
               {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
                {
                    // TODO Auto-generated method stub
                        if(!CheckBoxAutomat.isChecked())
                    { 
                            CheckBoxManual.setChecked(false);
    CheckBoxAutomat.setChecked(true);               


                    }

                }
             });
               //..............................
               CheckBoxManual.setOnCheckedChangeListener(new OnCheckedChangeListener() 
               {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
                {
                    // TODO Auto-generated method stub
                    if(!CheckBoxManual.isChecked())
                    { 
                        CheckBoxAutomat.setChecked(false);
    CheckBoxManual.setChecked(true);



                    }

                }
            });
Was it helpful?

Solution 2

I asked this question because I didnt like the defualt style of checkoboxes,while I needed radibutton surely.So I put radioGroup on my app and then change android:button with a checkbox_selector(with my favorite pictures ) which I created it. a cb_selector like this :

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:drawable="@drawable/checked2" />
    <item android:state_checked="false" android:drawable="@drawable/unchecked2" />
</selector>

this must write in a xml file in drawable forlder. then i write @drawable/cb_selector in Button property for each radiobutton in my radioGroup. see this my post How I can have behavior RadioGroup SingleChoice for 2 or more checkbox on android?(at here for 2 checkbox ,specially)

OTHER TIPS

Try with this :

CheckBoxAutomat.setOnCheckedChangeListener(new OnCheckedChangeListener() {

     @Override
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
     {
         // TODO Auto-generated method stub
             if(CheckBoxManual.isChecked()) {  // if Manual is checked uncheck it
                 CheckBoxManual.setChecked(false);
             }
     }
  });
    //..............................
    CheckBoxManual.setOnCheckedChangeListener(new OnCheckedChangeListener() {

     @Override
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
     {
         // TODO Auto-generated method stub
         if(CheckBoxAutomat.isChecked()) {  // if Automat is checked, uncheck it
             CheckBoxAutomat.setChecked(false);
         }
     }
 });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top