1

I make dynamic check box using below code in my project how i can get the check box id and make an command for it for example:

if(checkbox1.ischecked()==true){
//do 
}

Thank you in advance.

public void fetchContacts() {
    final LinearLayout Vll=(LinearLayout)findViewById(R.id.Vll);

    CheckBox cb = new CheckBox(getApplicationContext());
                        cb.setText(outputName+":"+outputNumber);
                        Vll.addView(cb);
}
Linh
  • 57,942
  • 23
  • 262
  • 279
mrreza
  • 23
  • 5

2 Answers2

1

you can do this like

cb.setId(anyPositiveInteger)

try this for more details How can I assign an ID to a view programmatically?

Community
  • 1
  • 1
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
0

You should store all references to all CheckBox in a ArrayList

List<CheckBox > allCheckBox = new ArrayList<CheckBox>();

And when you create any CheckBox, you need to add it to your List

CheckBox cb = new CheckBox(getApplicationContext());
...
allCheckBox.add(cb);

After that you can access to the CheckBox by

allCheckBox.get(position).isChecked() == true
// allCheckBox.get(position) -> with return a CheckBox
// position is the index when you add checkbox to ArrayList, for example you have create 2 CheckBox dynamic
// then CheckBox1 is allCheckBox.get(0) and CheckBox2 is allCheckBox.get(1)
Linh
  • 57,942
  • 23
  • 262
  • 279