0

I have created check boxes programmatically.

Here is my code:

            llmain = (LinearLayout) findViewById(R.id.linearLayoutMain);
            lLayout = new LinearLayout[b];
            for (int j = 0; j < b; j++) {
                int x = 0;
                x = x + (j * 5);
                lLayout[j] = new LinearLayout(CheckBoxdemo.this);
                lLayout[j].setLayoutParams(new LayoutParams(
                        LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT));
                lLayout[j].setOrientation(LinearLayout.VERTICAL);
                llmain.addView(lLayout[j]);
                for (int i = x; i < x + 5; i++) {
                    if (x > a) {
                        break;
                    } else {
                        if (testArrayList.contains(idsplit[i])) {
                            cb = new CheckBox(CheckBoxdemo.this);
                            cb.setText(namesplit[i]);
                            cb.setId(i + 1);
                            cb.setChecked(true);
                            cb.setTextColor(Color.BLACK);
                            cb.setTextSize(12f);
                            cb.setButtonDrawable(R.drawable.checkbox);
                            cb.setPadding(35, 5, 25, 5);
                            cb.setTag(i + 1);
                            cb.setOnCheckedChangeListener(handleCheck(cb));
                            if ((count1.equals(1)) || (count1.equals(2))) {
                                cb.setEnabled(true);
                            } else {
                                cb.setEnabled(false);
                            }
                            lLayout[j].addView(cb);
                        } else {
                            cb = new CheckBox(CheckBoxdemo.this);
                            cb.setText(namesplit[i]);
                            cb.setId(i + 1);
                            cb.setTextColor(Color.BLACK);
                            cb.setTextSize(12f);
                            cb.setButtonDrawable(R.drawable.checkbox);
                            cb.setPadding(35, 5, 25, 5);
                            cb.setTag(i + 1);
                            cb.setOnCheckedChangeListener(handleCheck(cb));
                            if ((count1.equals(1)) || (count1.equals(2))) {
                                cb.setEnabled(true);
                            } else {
                                cb.setEnabled(false);
                            }
                            lLayout[j].addView(cb);
                        }
                    }
                }
            }
  • Now I have received value of 15 check boxes from the database.

Now, all the check boxes are non- selected. If I click on any 5 of them; then I want to get the value of these 5- selected check boxes all together in an array, on just a click of a button.

How can I implement this???????

1
  • Can you loop over the lLayout childViews (getChildAt(index)) ? cast them to Checkbox and get the values? Commented Jun 20, 2014 at 7:31

4 Answers 4

3

XML Layout

<LinearLayout
    android:id="@+id/checkbox_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >
</LinearLayout>

Add CheckBox Dynamically

 LinearLayout checkBocContainer=  (LinearLayout)findViewById(R.id.checkbox_container);
    for (int j = 0; j < 5; j++)
    {
      CheckBox checkBox = new CheckBox(this);
      checkBox.setText("Check Box " + String.valueOf(j));
      checkBox.setId(j);
      checkBocContainer.addView(checkBox);
    }

Get Selected Checkbox Id

 ArrayList<Integer> listOfSelectedCheckBoxId=new ArrayList();
LinearLayout checkBocContainer=(LinearLayout)findViewById(R.id.checkbox_container);
for (int i = 0; i < checkBocContainer.getChildCount(); i++) 
 {
  CheckBox checkbox = (CheckBox) checkBocContainer.getChildAt(0);
   if (checkbox.isChecked())
    {
     listOfSelectedCheckBoxId.add(checkbox .getId());
    }
 }
Sign up to request clarification or add additional context in comments.

Comments

2

First of all instead of initiating same CheckBox object again and again, create Array of Checkbox

  CheckBox[] cbs;

Also create array of int variables (you can use Vector/ArrayList here)

  ArrayList<Integer> selectedCheckboxes = new ArrayList<Integer>(); // here change Integer as per data type of Tag (by default its int)

Before for loop, initialize this array with number of checkboxes you want to add in LinearLayout

  cbs = new CheckBox[<<put size here>>];

Now in for loop, initialize each checkbox

  cbs[<<put index variable here>>] = new CheckBox(CheckBoxdemo.this);

Example:

  cbs[i] = new CheckBox(CheckBoxdemo.this); //here i is iteration variable
  cbs[i].setText(namesplit[i]);
  cbs[i].setTag(i + 1);
  cbs[i].setId(i + 1);
  cbs[i].setChecked(true);
  cbs[i].setTextColor(Color.BLACK);
  cbs[i].setTextSize(12f);

In Click listener event of your button run loop with size of CheckBox array and check if each of checkbox is selected or not. If checkbox is selected then add its TAG value in ArrayList we have created

  for(int i = 0; i < cbs.lenght(); i++){
      if(cbs[i].isChecked()){
          selectedCheckboxes.add(cbs[i].getTag());
      }
  }

You will get values of all selected checkboxes in arraylist.

2 Comments

hello @silawar i got the value of checked check box as true in the logcat..but how should i get the position of the checked check boxes. if i click on 1,5,8 check boxes ..i should get their position as {1,5,8}
I was confused about your implementation of for loop and on which position you have added each checkbox. Just check if you are getting Tag of selected checkboxes only. If so, you are on right path and you can access values from each checkbox by running another loop on selectedCheckboxes arraylist
2

You can use ArrayList. Declare an ArrayList with global access and initialize it. And in setOnCheckedChangeListener() add/remove the checked box value in the array.

[EDIT]

For instance, I used your code, and every time check/uncheck, the list is updated. You can only see "checked" items

enter image description here

[EDIT 2]

The code in case you need it:

cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if(isChecked) {
        list.add("" + buttonView.getText().toString());
    } else {
        list.remove(list.indexOf(buttonView.getText().toString()));
    }
    String items = "";
    for (String item : list) {
        items += "" +item;
    }
    Log.i("", items);
}
});

2 Comments

can you explain... i used the onCheckedChangeListener and get the value of single clicked checkbox at a time in toast....
Every checkbox will be registered to a listener. So in your handlecheck method, write a condition: if(checked) { //add to list } else { //remove from list }
1
LinearLayout checkBoxContainerLayout= (Linearlayout)findViewById(R.id.checkbox_container);
ArrayList<String> al=new Arraylist(); 
for(int i=o;i< checkBoxContainerLayout.getChildCount();i++)
{
CheckBox checkbox=(CheckBox)checkBoxContainerLayout.getChildAt((0));
   if(checkbox.isChecked())
     {
      al.add(checkbox.getId());
     }
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.