Full tutorial is given below. Dear Viewer we are working hard to maintain this website. We doesn't want any think from you. Its just a humble request If you thinks that this posts helps you please share this post with your friends.
How to create a MultiChoice AlertDialog in Android
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="20dp" android:orientation="vertical" tools:context=".MainActivity"> <Button android:id="@+id/showsnackbarbtn" android:text="Show Alert Dialog" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/txtView" android:text="Preferred colors" android:textSize="20sp" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
MainActivity.java
package com.usmtip.myapplication; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import java.util.Arrays; import java.util.List; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final TextView textView = findViewById(R.id.txtView); Button showDialogBtn = findViewById(R.id.showsnackbarbtn); showDialogBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); // String array for alert dialog multi choice items String[] colorsArray = new String[]{"Black", "Orange", "Green", "Yellow", "White", "Purple"}; // Boolean array for initial selected items final boolean[] checkedColorsArray = new boolean[]{ false, // Black checked false, // Orange false, // Green false, // Yellow checked false, // White false //Purple }; // Convert the color array to list final List<String> colorsList = Arrays.asList(colorsArray); //setTitle builder.setTitle("Select colors"); //set multichoice builder.setMultiChoiceItems(colorsArray, checkedColorsArray, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { // Update the current focused item's checked status checkedColorsArray[which] = isChecked; // Get the current focused item String currentItem = colorsList.get(which); // Notify the current action Toast.makeText(getApplicationContext(), currentItem + " " + isChecked, Toast.LENGTH_SHORT).show(); } }); // Set the positive/yes button click listener builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // Do something when click positive button textView.setText("Your preferred colors..... \n"); for (int i = 0; i<checkedColorsArray.length; i++){ boolean checked = checkedColorsArray[i]; if (checked) { textView.setText(textView.getText() + colorsList.get(i) + "\n"); } } } }); // Set the neutral/cancel button click listener builder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // Do something when click the neutral button } }); AlertDialog dialog = builder.create(); // Display the alert dialog on interface dialog.show(); } }); } }
0 Comments: