Let's see how we can add Checkboxes to an Alert Dialog box, we will create multiple checkboxes that can be selected with an OK and Cancel button with the help of setMultiChoiceItems listener on AlertDialog.Builder object.

Alert Dialog with CheckBoxes Example
Steps 1 : Create a Dialog object
Step 2 : Create a String Array to hold Items for Checkboxes and an ArrayList to hold items selected
Step 3 : Create AlertDialog.Builder object
Step 4 : Set the Title for AlertDialog
Step 5 : Now on builder object call setMultiChoiceItems
Step 6 : Set setPositiveButton and setNegativeButton button actions.
Step 7 : set dialog = builder.create(); and to display it call dialog.show();
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import java.util.ArrayList;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Dialog dialog;
final String[] items = {" PHP", " JAVA", " JSON", " C#", " Objective-C"};
final ArrayList itemsSelected = new ArrayList();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select Languages you know : ");
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();
}
}
More Posts related to Android,
- Check Internet Connection WIFI 4G is active on Android Programmatically
- Android Emulator cannot be opened because the developer cannot be verified. [M1 Mac]
- How to Change Android Toast Position?
- Fail to connect to camera service Android java RuntimeException
- How to create Custom RatingBar Android Programming Tutorial
- Fixing Android unknown error 961 while downloading app
- Android AlertDialog with Yes No and Cancel Button
- Share or Send SMS via Android Intent
- The Android Virtual Device myEmulator is currently running an emulator and cannot be deleted.
- Pass data between two Android Activities and access it using Intent
- SQLite with Android Easy to Understand Tutorial that covers Select, Insert, Update and Delete
- [FIX] AndroidRuntime: FATAL EXCEPTION: main - java.lang.RuntimeException NullPointerException
- Android EditText Cursor Colour appears to be white
- Android Development - How to switch between two Activities
- Android xml error Attribute is missing the Android namespace prefix [Solution]
- Android : Remove ListView Separator/divider programmatically or using xml property
- Android is starting optimizing... app 1 of 1
- java.lang.NoClassDefFoundError android.support.v4.content.LocalBroadcastManager
- AlertDialog with single button example : Android
- Android : Exception raised during rendering: action_bar API 22
- Maven : java.lang.ClassNotFoundException: Xmx512m
- Android Lint app_name is not translated in af (Afrikaans) am (Amharic) ar (Arabic) bg (Bulgarian)
- Center align text in TextView Android Programming
- How to Download and Install Android adb Tool on Linux, Mac or Windows
- Multiline EditText in Android Example
More Posts:
- Fix: Sorry, we cant sync this folder, Youre already syncing a shortcut to a folder from this shared library [OneDrive] - Windows
- Know installed PowerShell Version on Mac - Powershell
- How to track and download Microsoft Teams meeting attendance report of participants/students - Teams
- reCAPTCHA Implementation Tutorial - CSS
- How to create Toast messages in Android? - Android
- How to get current URL Location using Javascript HTML - JavaScript
- Python: Replace the First Occurrence of String in a String - Python
- How to Show Path of Files or App in Mac Spotlight Search - MacOS