nAkhmedov
nAkhmedov

Reputation: 3592

How to create AlertDialog with single choice checkbox in android?

How to implement single choice with checkboxes in AlertDialog? I try to implement with setSingleChoiceItems, but it displays radio button. setMultiChoiceItems displays checkboxes but there user can check multiple items. I need to implement singleChoice with Checkboxes. I need yr help?

Upvotes: 0

Views: 895

Answers (1)

Pararth
Pararth

Reputation: 8134

Programmatically, with only two checkboxes, you can write a switch case or if-else loop with the onCheckChangedListener like;

public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
if(isChecked){
switch(arg0.getId())
  {
    case R.id.cbOne:
         cbOne.setChecked(true);
         cbTwo.setChecked(false);
         break;
    case R.id.cbTwo:
         cbTwo.setChecked(true);
         cbOne.setChecked(false);
         break;

  }
}

And for both checkbox items you can set the listener as:

yourCb = (CheckBox)findViewById(R.id.yourCb);
yourCb.setOnCheckedChangeListener(yourListener);

If you customise the background, the default behaviour would also suit your requirement.. custom-android-checkbox-radiobutton

Upvotes: 3

Related Questions