Articles → Android → Multiple Choice Alert Dialog In Android
Multiple Choice Alert Dialog In Android
Example
- Create a button
Click to Enlarge
- Create an array in resources
Click to Enlarge
- Write code for button onclick event
public void ButtonClicked(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final String[] availableTypes = getResources().getStringArray(R.array.sample_array);
builder.setMultiChoiceItems(R.array.sample_array, null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int item, boolean isChecked) {
if (isChecked) {
Toast.makeText(EditTextDemo.this, availableTypes[item], Toast.LENGTH_LONG).show();
}
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(EditTextDemo.this, "Ok Button Clicked", Toast.LENGTH_LONG).show();
}
});
builder.show();
}
Output
Click to Enlarge
Click to Enlarge