Skip to content Skip to sidebar Skip to footer

Check The State Of Checkboxes Which Were Added Dynamically

I add my checkboxes dynamically like this: public void addFiles() { LinearLayout layout = (LinearLayout) findViewById(R.id.filesList); if(!FileM

Solution 1:

your can add your checkboxes like:

for(int i=0;i<totalCB;i++){
    CheckBox chB=new CheckBox(context);
    ...
    chB.setId(i);
    layout.add(...);// add checkbox to view
}

now,on click of button,

for(int i=0;i<totalCB;i++){
    CheckBox cb=(CheckBox)findViewById(i);
    boolean checked=cd.isChecked();// status of checkboxif(checked){
       // perform action 
    }
}

Solution 2:

for(int i=0;i<layout.getChildCount;i++){
   if ( ((CheckBox) layout.getChildAt(i)).isChecked() )
                 //remove the Box
}

This should do the trick. You may have to alter the code if you have other Views in your layout or create a new layout which only hosts the checkboxes. The Code goes into the OnClick Method of you Buttons onclicklistener of course.

Solution 3:

You can set id for the checkbox using informationView.setId(someUniqueInt). and then find a text box whereever you want in the activity by

CheckBox checkBox = (CheckBox) findViewById(someUniqueInt)

And then use

if (checkBox.isChecked()) {
     //do whatever you want here
}

Post a Comment for "Check The State Of Checkboxes Which Were Added Dynamically"