Home > Net >  How to put only one tick from multiple checkbox in android?
How to put only one tick from multiple checkbox in android?

Time:03-11

<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.

CodePudding user response:

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)
                    }
                    }
                });

CodePudding user response:

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

CodePudding user response:

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.

  • Related