Kris
Kris

Reputation: 921

How to uncheck checkbox using tag value of another checkbox in android

I have a number of check boxes creating dynamically.

Now i just want to select any of them not more than one. When i select the second one the first one should unselect.

I can't use radio group. Any help

Here is my code .. i am creating check boxes dynamically

private void createRadioButton() { cb = new CheckBox(fContext);

    cb.setTag(id++);
    cb.setOnCheckedChangeListener( new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
             int getPosition = (Integer) buttonView.getTag();
             Log.i("comVisa","getPos =="+getPosition);

        }
    });


}

Upvotes: 0

Views: 890

Answers (2)

AwadKab
AwadKab

Reputation: 3064

You can save last check box checked and unchecked it when checked other one like this

In public area define this

HashMap<String, CompoundButton> hash = new HashMap<String, CompoundButton>();

then edit your code :

cb.setTag(id++);

cb.setOnCheckedChangeListener( new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
     //int getPosition = (Integer) buttonView.getTag();
     if(isChecked){
         if(hash.size()>0){
                hash.get("1").setChecked(false);
             }
             hash.put("1", buttonView);


 }else{
hash.clear();
}
     Log.i("comVisa","getPos =="+getPosition);

}
});

Upvotes: 1

KDeogharkar
KDeogharkar

Reputation: 10959

A really simple example of using single selection checkbox.
try this link


CheckBox myCheckBox[] = new CheckBox[noofCheckBox];
    for (int i=0; i<noofCheckBox; i++) {
        myCheckBox[i] = new myCheckBox(this);
        myCheckBox[i].setId(i+1);
        myCheckBox[i].setOnCheckedChangeListener( new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
             int getPosition = (Integer) buttonView.getTag();
             if(myCheckBox[i].getId() == getPosition){
                myCheckBox[i].setChecked(true);
             }else{
               myCheckBox[i].setChecked(false)
             }

        }
    });
    }

Upvotes: 1

Related Questions