user3051460 user3051460 - 14 days ago 5
Android Question

How to make single checked of checkbox in a Dialog Android?

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>

Answer

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

    }
});}