dimanche 10 octobre 2021

get id of checbox in checkbox group android

I have a dynamic checkbox group which in my particular example has five checkboxes. I am trying to get the id of clicked checkbox, but it is only working for the last checkbox in that group.

    for (int index = 0; index < array.get(index).length; index++) {
                                    
             checkBox = new CheckBox(this);
             checkBox.setId(index);
             checkBox1.setText("cb");
             layout.addView(checkBox);
                                      
       }

checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
          @Override
          public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
               if (isChecked) {
                   checkbox_id = buttonView.getId();
                   Toast.makeText(this , checkbox_id, Toast.LENGTH_SHORT).show();
               }
      });

The above code is working for only the last checkbox meaning that there is only one toast for the fifth checkbox of "4". How can I get any id of clicked box?




Aucun commentaire:

Enregistrer un commentaire