Kris
Kris

Reputation: 921

Force dynamically generated checkboxes to allow only one to be selected

I am generating a series of checkboxes dynamically.

enter image description here

I can check any one of the checkbox from that:

enter image description here

How can you uncheck the checked checkbox when clicking on another checkbox?

edited

here i am adding my RadioGroup implementation and i am getting IllegalStateException final LinearLayout firstRowTxtLayout = new LinearLayout(fContext); firstRowTxtLayout.setLayoutParams(new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));

    rbGroup.setLayoutParams(new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT));
    rbButton = new RadioButton(fContext);
    rbButton.setLayoutParams(new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT));
    rbButton.setId(rbTagincreament);
    rbGroup.addView(rbButton);

then i am adding this radioGroup in to another main linear layout

firstRowTxtLayout.addView(rbGroup); here i am getting the exception

Upvotes: 1

Views: 1600

Answers (3)

Amit Gupta
Amit Gupta

Reputation: 8939

enter image description here

I used RadioGroup with custom RadioButton but i changed RadioButton to Checkbox. It will behave like a RadioButton only i.e only single selection.

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linearlayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </LinearLayout>

</LinearLayout>

MainActivity.java

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        LinearLayout linear=(LinearLayout) findViewById(R.id.linearlayout);

        final RadioButton[] rb = new RadioButton[5];
        RadioGroup rg = new RadioGroup(this); //create the RadioGroup
        rg.setOrientation(RadioGroup.VERTICAL);//or RadioGroup.VERTICAL
        for(int i=0; i<5; i++){
            rb[i]  = new RadioButton(this);
            rb[i].setButtonDrawable(R.drawable.single_radio_chice);
            rg.addView(rb[i]); 

        }
        LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
        );
        p.setMargins(10, 10, 10, 10);
        linear.addView(rg,p);

    }

selector

single_radio_chice

<?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_checked="false" 
        android:drawable="@drawable/check_off" />

    <item android:state_checked="true" 
        android:drawable="@drawable/check_on" />
</selector>

Hope this will help you.

Upvotes: 5

Ajay S
Ajay S

Reputation: 48602

Get the reference of all checkbox in different variable.

satView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

   @Override
   public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
          if(isChecked) {
            checkBox.setChecked(false); //Here call this method on all checkbox except you want to check single checkbox.
        }
   }
}

But better approach is use RadioGroup and RadioButton because Radio buttons allow the user to select one option from a set. Read here.

Upvotes: 2

Emil Adz
Emil Adz

Reputation: 41119

Put all your dynamically created RadioButtons into array. and when you check one of them go over all the others and setChecked(false);

As mentioned RadioGroup can be a solution too. but for some reason I found it hard to use if you want to put other objects like ImageViews or TextView between the RadioButtons.

Upvotes: 1

Related Questions