0

I have two checkbox in a Dialog. I want to set single check for these checkboxes. It means if I checked the checkbox 1, the checkbox 2 will be unchecked. Similar when I check the checkbox 2. The check_value variable is set true if the checkbox 1 is checked and false when the checkbox 2 is check.

This is my code but it cannot achieve as my expected

@Override
public Dialog onCreateDialog(final Bundle savedInstanceState) {
    final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    final View dialogView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_dialogView, null);
    boolean check_value=false;

    checkbox1 = (CheckBox) dialogView.findViewById(R.id.checkbox1);
    checkbox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            checkbox2.setChecked(false);
            checkbox1.setChecked(true);
            check_value=true;
        }
    });
    checkbox2 = (CheckBox) dialogView.findViewById(R.id.checkbox2);
    checkbox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            checkbox2.setChecked(true);
            checkbox1.setChecked(false);
            check_value=false;
        }
    });
}

This is my xml file. The checkbox1 is true as default

<LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_below="@+id/list"
        android:layout_centerInParent="true">
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/checkbox1"
        android:checked="true"
        android:layout_marginLeft="2dp"/>

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/checkbox2"
        android:checked="false"
        android:layout_marginLeft="2dp" />
</LinearLayout>
user3051460
  • 1,455
  • 22
  • 58
  • why checkboxes and not radiobuttons? they are born for this purpose – Pier Giorgio Misley Nov 22 '16 at 13:30
  • Could you answer my question by using radiobutton? Note that, I am put them in a dialog – user3051460 Nov 22 '16 at 13:33
  • Sorry, now i don't have a compiler. just have a look at [This post on how to use custom layouts in builders](http://stackoverflow.com/questions/22655599/alertdialog-builder-with-custom-layout-and-edittext-cannot-access-view) and you can also refer to [this link also](http://stackoverflow.com/questions/27785167/radio-buttons-in-android-studio) for how to use radiobuttons. hope this helps – Pier Giorgio Misley Nov 22 '16 at 13:46

4 Answers4

1

Try this:

    @Override
public Dialog onCreateDialog(final Bundle savedInstanceState) {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final View dialogView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_dialogView, null);
boolean check_value=false;

checkbox1 = (CheckBox) dialogView.findViewById(R.id.checkbox1);
checkbox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(isChecked){
           checkbox2.setChecked(false);
           check_value=true;
        }
        else
           checkbox2.setChecked(true);
    }
});
checkbox2 = (CheckBox) dialogView.findViewById(R.id.checkbox2);
checkbox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(isChecked){
           checkbox1.setChecked(false);
           check_value=false;
        }
        else
           checkbox1.setChecked(true);

    }
});}
Vygintas B
  • 1,624
  • 13
  • 31
0

You should use a radio group with 2 radio buttons. That has the default functionality what you are looking for. Checkboxes are not meant for this.

Madhav
  • 283
  • 3
  • 13
0

In Android, you can use “android.widget.CheckBox” class to render a checkbox.

In this tutorial, we show you how to create 3 checkboxes in XML file, and demonstrates the use of listener to check the checkbox state – checked or unchecked.

https://www.mkyong.com/android/android-checkbox-example/

0

Do like code below...

<RadioGroup
        android:id="@+id/myRadioGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5sp"
        android:orientation="horizontal">
        <RadioButton
            android:id="@+id/rbTrue"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Accept"/>
        <RadioButton
            android:id="@+id/rbFalse"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Reject"/>
    </RadioGroup>

Then in activity do like...

Here radioGroup is object of RadioGroup which consists radio buttons

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if(checkedId==R.id.rbTrue){
                check_value = true;
            }else{
                check_value = false;
            }
        }
    });