Skip to content Skip to sidebar Skip to footer

How To Add Action Dynamically For Multiple Checkbox On Android?

In my project rows are added dynamically to the table depending on selected items from database. This table contains a column with checkbox How can I set action for each checkbox t

Solution 1:

What you can do is:

  1. As you create each checkbox, give it a unique tag by using setTag(Object tag) method.

  2. Set the same OnClickListener for each checkbox.

  3. Inside onClick(View v) method, get the tag by using v.getTag(). This way you will know which checkbox was clicked.


Solution 2:

I would say yes.

FrameLayout yourlayout = (FrameLayout) findViewById(R.id.container);
    CheckBox[] check = new CheckBox[10];
    check[0] = new CheckBox(this);

    yourlayout.addView(check[0]);

    check[0].setOnClickListener(new OnClickListener() {
        public void onClick(View v) {

            Toast.makeText(getApplicationContext(), "Hi, this is working fine", Toast.LENGTH_SHORT).show();
        }
    });

I don't know what kind of layout you are using, but this example works for me.

Edit: as said above, setting a tag is a nice way to know what checkbox was pressed.


Post a Comment for "How To Add Action Dynamically For Multiple Checkbox On Android?"