Latest News

How To Create A Multichoice Alertdialog In Android

In this tutorial we will create a Multiple Choice Alert Dialog using android studio. We will show list of colors to show in dialog. We will create a button to show that AlertDialog , and a TextView to show the selected items...

Step 1: Create a new project OR Open your project

Step 2: Code:

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"><Buttonandroid:id="@+id/showsnackbarbtn"android:text="Show Alert Dialog"android:layout_width="match_parent"android:layout_height="wrap_content" /><TextViewandroid: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.blogspot.devofandroid.myapplication;import android.content.DialogInterface;import android.os.Bundle;import android.support.v7.app.AlertDialog;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import android.widget.TextView;import android.widget.Toast;import java.util.ArrayList;import java.util.Arrays;import java.util.List;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(final 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() {
@Overridepublic void onClick(View view) {AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);// String array for alert obrolan multi choice itemsString[] colorsArray = new String[]{"Black" , "Orange" , "Green" , "Yellow" , "White" , "Purple"};// Boolean array for initial selected itemsfinal boolean[] checkedColorsArray = new boolean[]{true , // Black checkedfalse , // Orangefalse , // Greentrue , // Yellow checkedfalse , // Whitefalse  //Purple};// Convert the color array to listfinal List<String> colorsList = Arrays.asList(colorsArray);//setTitlebuilder.setTitle("Select colors");//set multichoicebuilder.setMultiChoiceItems(colorsArray , checkedColorsArray , new DialogInterface.OnMultiChoiceClickListener() {@Overridepublic void onClick(DialogInterface dialog , int which , boolean isChecked) {// Update the current focused item's checked statuscheckedColorsArray[which] = isChecked;// Get the current focused itemString currentItem = colorsList.get(which);// Notify the current actionToast.makeText(getApplicationContext() , currentItem + " " + isChecked , Toast.LENGTH_SHORT).show();}});// Set the positive/yes button click listenerbuilder.setPositiveButton("OK" , new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog , int which) {// Do something when click positive buttontextView.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 listenerbuilder.setNeutralButton("Cancel" , new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog , int which) {// Do something when click the neutral button}});AlertDialog obrolan = builder.create();// Display the alert obrolan on interfacedialog.show();}});}}

Step3: Output


1 Response to "How To Create A Multichoice Alertdialog In Android"