0
<LinearLayout
                    android:id="@+id/checkBoxGroup"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <CheckBox
                        android:id="@+id/yes"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:text="@string/yes" />

                    <CheckBox
                        android:id="@+id/no"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:text="@string/no" />
                </LinearLayout>

here I want to tick for only one checkbox. if I ticked one other should be unticked.

nim
  • 85
  • 2
  • 11
  • 1
    Why not just use `RadioGroup` with `RadioButtons`? that's the motive of this widget .. – ADM Mar 11 '22 at 06:08
  • yes, But here I want to use checkBoxes – nim Mar 11 '22 at 06:11
  • Then handle the state by yourself with code . its not a big deal with two check boxes . Put checked change listener on Both and unchecked the other one if one is checked . You can also try to use Radio Button with Check Box Style . not sure if it will work or not .. – ADM Mar 11 '22 at 06:12

3 Answers3

1

You can use the radio button instead of a checkbox. and if you want to use the checkbox.

 cbYes.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                    if (cbNo.isChecked){
                        cnNo.setCheck(false)
                    }
                    }
                });
 cbNo.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                    if (cbYes.isChecked){
                        cbYes.setCheck(false)
                    }
                    }
                });
Abhi S
  • 250
  • 2
  • 18
0

As mentioned by @ADM radiogroup would be the preferred implementation..

But if you have to use a checkbox. Use this logic

  1. Have an array for the no of checkboxes [false,false,false,false]

  2. Now let's say you clicked on the second checkbox [false,true,false,false]

  3. Now when you click on 4th checkbox. It should change the fourth false to true and the others to false

  4. Now set the checkbox according to the array

.

Or you could use radiogroup :P as mentioned before which handles state by itself

Narendra_Nath
  • 4,578
  • 3
  • 13
  • 31
0

You can put multiple radioButtons inside RadioGroup. You have to just set button attribute to customise it like below,

<RadioGroup>
   <androidx.appcompat.widget.AppCompatRadioButton
                            android:id="@+id/cbYes"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:button="@drawable/radio_button_selector"
                            android:text="@string/yes" />
        
  <androidx.appcompat.widget.AppCompatRadioButton
                            android:id="@+id/cbNo"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:button="@drawable/radio_button_selector"
                            android:text="@string/no" />
</RadioGroup>

radio_button_selector.xml

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

radio_button_selected & radio_button_unselected will be checkbox drawables/images.

Jinal Patel
  • 229
  • 2
  • 7