Fcoder
Fcoder

Reputation: 9216

isChecked in dynamically created checkboxes

I created some checkboxed dynamically in runtime, now i want to know ehen one of them ckecked or not. how i can do this?

for (int i = 0; i < cnt; i++) {
                    cb = new CheckBox(getApplicationContext());
                    TextView txt = new TextView(getApplicationContext());
                    ll2 = new LinearLayout(
                            PollActivity.this);
                    ll2.setOrientation(LinearLayout.HORIZONTAL);

                    ll2.addView(cb);

                    ll.addView(ll2);

                }

Upvotes: 0

Views: 95

Answers (3)

Simon Dorociak
Simon Dorociak

Reputation: 33505

I suggest you to set(in loop) for each CheckBox set OnCheckedChangeListener.

cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
   public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
      if (isChecked) {
         // do some action
      }     
   }
});

Now you are able to handle events for each CheckBox.

Upvotes: 1

AndroGeek
AndroGeek

Reputation: 1300

Just check cb.isChecked() which returns a boolean value.

Upvotes: 0

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132982

you will need to set CompoundButton.OnCheckedChangeListener for CheckBox to fire an event when checkbox clicked. Example :

cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

   @Override
   public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {

      // do your code here
   }
});

Upvotes: 1

Related Questions