Android Tutorial
Software Setup and Configuration
Android Studio
File Structure
Components
Core Topics
Layout
View
Button
Intent and Intent Filters
Toast
RecyclerView
Fragments
Adapters
Other UI Component
Image Loading Libraries
Date and Time
Material Design
Bars
Working with Google Maps
Chart
Animation
Database
Advance Android
Jetpack
Architecture
App Publish
App Monetization
To create an AlertDialog with multiple item selection in Android using the traditional View system, you typically use a CharSequence[]
array for your list of items and a boolean[]
array to keep track of which items are checked.
Here's how to create such an AlertDialog
:
For simplicity, we will create a Button in activity_main.xml
:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp"> <Button android:id="@+id/showDialogButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Show Dialog" android:layout_centerInParent="true" /> </RelativeLayout>
In your MainActivity.java
:
import android.app.AlertDialog; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { private Button showDialogButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final CharSequence[] items = {"Item 1", "Item 2", "Item 3", "Item 4"}; final boolean[] selectedItems = new boolean[items.length]; showDialogButton = findViewById(R.id.showDialogButton); showDialogButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("Choose some items"); builder.setMultiChoiceItems(items, selectedItems, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { selectedItems[which] = isChecked; } }); builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { StringBuilder selected = new StringBuilder(); for (int i = 0; i < items.length; i++) { if (selectedItems[i]) { selected.append(items[i]).append("\n"); } } Toast.makeText(MainActivity.this, "Selected items:\n" + selected.toString(), Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("Cancel", null); builder.show(); } }); } }
In this example, clicking the button shows an AlertDialog
with multiple selectable items. After making selections and pressing "OK", a toast message displays the selected items. The boolean[] selectedItems
array keeps track of which items are currently checked.
You can modify the items and actions as needed to suit your requirements.
Implementing multi-choice items in AlertDialog Android:
In Android, you can create an AlertDialog
with multi-choice items using the setMultiChoiceItems
method. Here's an example:
AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Choose items") .setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { // Handle item click and update checkedItems array } }) .setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // Handle positive button click } }) .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // Handle negative button click } }) .show();
AlertDialog with ListView for multiple item selection in Android:
You can use a ListView
with an adapter for multiple item selection in an AlertDialog
. Here's an example:
AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Choose items") .setAdapter(adapter, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // Handle item click } }) .setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // Handle positive button click } }) .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // Handle negative button click } }) .show();
How to get selected items from AlertDialog in Android:
To get the selected items from an AlertDialog
with multi-choice items, use the getCheckedItems
method. For example:
boolean[] checkedItems = // your array builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { SparseBooleanArray checkedItemPositions = ((AlertDialog) dialog).getListView().getCheckedItemPositions(); for (int i = 0; i < checkedItemPositions.size(); i++) { if (checkedItemPositions.valueAt(i)) { int position = checkedItemPositions.keyAt(i); // Handle the selected item at position } } } });