0

Iam creating a dialog with following code, who creates multiple choice check box.. But I don't know how to create their id's to add click event , I m new to android please help me..:

private void showDailog() {
final String[] items = {" Blue", " Red", " Black", " White", " Pink"};
final ArrayList itemsSelected = new ArrayList();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select any theme you want : ");
builder.setMultiChoiceItems(items, null,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int selectedItemId,
boolean isSelected) {
if (isSelected) {

itemsSelected.add(selectedItemId);
} else if (itemsSelected.contains(selectedItemId)) {

itemsSelected.remove(Integer.valueOf(selectedItemId));
}
}
})
.setPositiveButton("Done!", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
//Your logic when OK button is clicked
}
})
.setNegativeButton("Cancel", new     DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) 
}
});
dialog = builder.create();
dialog.show();
}
Niezwm
  • 87
  • 1
  • 8
  • To generate ids you can use `View.generateViewId()`, reference [here](https://developer.android.com/reference/android/view/View.html#generateViewId()), same question [here](https://stackoverflow.com/a/21000252/2910520) – MatPag Jul 14 '17 at 12:48

1 Answers1

0

Instead of Alert Dialog create a simple dialog with your custom layout like this

   Dialog dialog = new Dialog(MainActivity.this);
    dialog.setContentView(R.layout.dialog_lauout);
    dialog.show();
    Button button = (CheckBox) dialog.findViewById(R.id.button);
    checkBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    }); 
Anmol317
  • 1,356
  • 1
  • 14
  • 31