Home > Android Development, Tips & Tricks > Allow single selection on Android checkbox

Allow single selection on Android checkbox

December 2nd, 2011 Leave a comment Go to comments

You have RadioButton using RadioGroup in your android application to allow the user to select only one choice at a time, but you may want the same using CheckBox. There is no CheckBoxGroup thing exists. By default, the checkboxes are not grouped.

If you have X checkboxes in your application you can select all of them, but if you want to use the checkbox and allow only one selection you’ve to do it manually.

First of create checkboxes in your design file.


<CheckBox
android:id="@+id/cbActivate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:text="@string/ActivateMe" />

<CheckBox
android:id="@+id/cbAll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:text="@string/ApplyAll" />

<CheckBox
android:id="@+id/cbFilter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:text="@string/ApplyFilter" />

and here is the Activity class code:

public class SingleSelectionCheckBox extends Activity {

 CheckBox cbActivate ;
 CheckBox cbAll ;
 CheckBox cbFilter ;

public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);

 cbActivate = (CheckBox)findViewById(R.id.cbActivate);
 cbActivate.setOnCheckedChangeListener(listener);

cbAll = (CheckBox)findViewById(R.id.cbAll);
 cbAll.setOnCheckedChangeListener(listener);

cbFilter = (CheckBox)findViewById(R.id.cbFilter);
 cbFilter.setOnCheckedChangeListener(listener);

}&lt;/pre&gt;
&lt;!--more--&gt;
&lt;pre&gt;
private OnCheckedChangeListener listener = new OnCheckedChangeListener() {

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

}
};

You can even register different listeners for each CheckBox and move the appropriate ‘case’ statements to that listener.

So you can modify/optimize the above code to have more control over your checkboxes.

  1. Pritamde
    July 8th, 2012 at 16:33 | #1

    something is wrong with the code. Not getting any errors. at the same time all checkboxes are clickable.

  2. mrsaqib
    July 10th, 2012 at 00:04 | #2

    Pritamde :

    something is wrong with the code. Not getting any errors. at the same time all checkboxes are clickable.

    Strange!! The code is pretty straight forward…
    You can send me the code, I’ll check…

  3. ty
    April 6th, 2013 at 00:08 | #3

    yea this doesnt work. Nullpointers when setCheck(true/false)

  4. ty
    April 6th, 2013 at 00:12 | #4

    @ty
    it works, nevermind. thanks

  1. No trackbacks yet.