How to create alert Dialog with checkbox in Android

How to create alert Dialog with checkbox in Android

In Android Application, There have lots of time you need to show popup option with checkboxes. Most of the time this required for multiple value selection functionalities in your application. In this blog, I will give you the complete process of integrating proper dialogue box in your application with a checkbox.  So, Follow the step given below to integrate popup menu.

Step 1: Give below system permissions into AndroidManifest.xml File

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

Step 2: Create Dialog with below code

Dialog dialog;
//Dialog Options
final String[] items = {"Option 1", "Option 2", "Option 3", "Option 4", "Option 5"};
final ArrayList itemsSelected = new ArrayList();
AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this);
builder.setTitle("Select Option: ");
builder.setMultiChoiceItems(items, null,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int selectedItemId,
boolean isSelected) {
if (isSelected) {
itemsSelected.add(items[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();

READ ALSO

Best Book for Entrepreneur ==> Strategies To Build Successful Web Agency

Related Posts

3 Responses to "How to create alert Dialog with checkbox in Android "

Iklan Atas Artikel

Advetisement

Advertisement

Iklan Bawah Artikel