Skip to content Skip to sidebar Skip to footer

Add Onclick Action Listener For Dynamically Created Checkbox

I am creating CheckBoxes Dynamically in an Android Class (Not Activity). So I need to add onClick Action Listener to my checkboxes, how to implement this. I am using following code

Solution 1:

you can also set any Listener to Views from non-activity class. try it as :

check[i].setId(100+i);
check[i].setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

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

      switch(buttonView.getId()) {
        case R.id.checkbox_meat:
         // do your code here...
          break;
        case R.id.checkbox_cheese:
         // do your code here...
          break;
       // TODO: Veggie sandwich
      }

   }
});

Post a Comment for "Add Onclick Action Listener For Dynamically Created Checkbox"