12

I have two radio buttons and 5 checkboxes in my android app. and also a save button. When the user clicks save button I need to uncheck the checkboxes checked by the user. I have tried with the following code.But it is not working.

if (chkOthers.isChecked()) 
    chkOthers.setChecked(false);
    chkOthers.setSelected(false);
}
2
  • 1
    What is the context of that snippet? It's too short to be useful.
    – Tim
    Commented Oct 30, 2012 at 5:48
  • 1
    Do this, if(chkOthers.isChecked()) { chkOthers.performClick(); } This will perform normal click operation on the checkbox, which will also perform the onClickListener(), So use it wisely
    – Hardik4560
    Commented Oct 30, 2012 at 7:09

4 Answers 4

19

Just use chk1.toggle() onClick of the button to uncheck the checked ones.

public class TestCheckBoxActivity extends Activity {
  /** Called when the activity is first created. */
     CheckBox chk1, chk2;

        @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        chk1 = (CheckBox)findViewById(R.id.checkBox1);
        chk2 = (CheckBox)findViewById(R.id.checkBox2);

        Button btn = (Button)findViewById(R.id.button1);

        btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if(chk1.isChecked()){
                chk1.toggle();
            }

            if(chk2.isChecked()){
                chk2.toggle();
            }

        }
    });
       }
}
10

If you want to use checkboxes for this, you can set an onItemClickListener on both the checkboxes and need to unselect other in the onItemClick() Method. An example would be like this:-

CheckBox cb1,cb2;
//Considering you can initialize the above variables
cb1.setOnCheckedChangeListener(new OnCheckedChangeListener{
    onCheckedChanged (CompoundButton view, boolean isChecked){
        cb2.setChecked(false);
    }
});
cb2.setOnCheckedChangeListener(new OnCheckedChangeListener{
    onCheckedChanged (CompoundButton view, boolean isChecked){
        cb1.setChecked(false);
    }
});

I would reccomend that you should use radio buttons for this behavior since they come with this functionality built in from the beginning.

0
   holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
               if(holder.checkBox.isChecked()) {
                   holder.checkBox.setChecked(false);
               }else {
                   holder.checkBox.setChecked(true);
               }
            }
        });
0
Button b = findViewById(R.id.{yourButtonID});
b.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        chk1.setChecked(false);
        chk2.setChecked(false);
        chk3.setChecked(false);
        chk4.setChecked(false);
        chk5.setChecked(false);
    }
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.