I have a multiple checkboxes, but I want user to select one checkbox only. I try if-else statement but it is not useable. So, I want to know, how to be like that.
Thanks
If you want to do a selectionable list where only one can be selected the best in term of UX is to use radio button.
Why don't you use Radio Buttons instead? They are meant exactly for that.
But if you insist on using check boxes, modify the event of on select of any check box such that when it is selected it disables the other check boxes.
I made this:
public void check_checkbox(){
int CB_count=0;
if (CB_1.isChecked()) {
CB_count=CB_count+1;
}if (CB_2.isChecked()) {
CB_count=CB_count+1;
}if (CB_3.isChecked()) {
CB_count=CB_count+1;}
if (CB_count == 1) {
//do your magic
} else {
Toast.makeText(getActivity(), "you only can select 1 checkbox", Toast.LENGTH_SHORT).show();
}
}
I improve it, on the onCreateView=
CB_1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b) {
CB_2.setChecked(false);
CB_3.setChecked(false);
}
}
});
CB_2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b) {
CB_1.setChecked(false);
CB_3.setChecked(false);
}
}
});
CB_3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b) {
CB_2.setChecked(false);
CB_1.setChecked(false);
}
}
});
I did this:
- In my layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alignmentMode="alignBounds"
android:columnCount="1"
android:background="#ff99f6ff"
>
<ImageView
android:id="@+id/escudo"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_gravity="center_horizontal|fill_vertical"
android:src="@drawable/dicon2"
android:contentDescription="foto logo"
android:layout_marginTop="30dp"
android:layout_alignParentTop="false"
android:layout_centerInParent="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_gravity="top"
android:layout_below="@+id/radioGroup"
android:layout_centerHorizontal="true"
android:layout_marginTop="48dp" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/radioGroup"
android:orientation="horizontal"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="34dp"
android:gravity="center_horizontal"
android:checkedButton="1"
>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/check_a"
android:id="@+id/checkBoxA"
android:layout_above="@+id/escudo"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="onCheckboxClicked" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/check_b"
android:id="@+id/checkBoxB"
android:layout_alignTop="@+id/checkBoxA"
android:layout_toRightOf="@+id/checkBoxA"
android:layout_toEndOf="@+id/checkBoxA"
android:onClick="onCheckboxClicked"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/check_c"
android:id="@+id/checkBoxC"
android:layout_alignTop="@+id/checkBoxB"
android:layout_toRightOf="@+id/checkBoxB"
android:layout_toEndOf="@+id/checkBoxB"
android:onClick="onCheckboxClicked"/>
</RadioGroup>
</RelativeLayout>
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
public class MainActivity extends ActionBarActivity {
private CheckBox checkBoxA, checkBoxB, checkBoxC;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkBoxA = (CheckBox) findViewById(R.id.checkBoxA);
checkBoxB = (CheckBox) findViewById(R.id.checkBoxB);
checkBoxC = (CheckBox) findViewById(R.id.checkBoxC);
}
public void onCheckboxClicked(View view) {
switch(view.getId()) {
case R.id.checkBoxA:
checkBoxB.setChecked(false);
checkBoxC.setChecked(false);
break;
case R.id.checkBoxB:
checkBoxC.setChecked(false);
checkBoxA.setChecked(false);
break;
case R.id.checkBoxC:
checkBoxA.setChecked(false);
checkBoxB.setChecked(false);
break;
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With