How to Implement MultiSelect DropDown in Android?
Last Updated :
30 Jan, 2022
In this article, we are going to see how we can make a MultiSelect DropDown in android studio and will select multiple items from a dropdown list. Advantages of MultiSelect DropDown.
- It is a good replacement for list boxes as it uses less space does the same work as a list box and gives a good look to UI.
- Dropdown lists are familiar selection mechanisms for most users since they are widely used both on the web and in android apps.
- User can select more than one item which he/she likes.
What we are going to build in this article?
In this article, we will be using a TextView, and we will set an onClickListener on that TextView so that whenever the user clicks on it dropdown list occurs. In the dropdown list, we will provide a feature to select multiple items, clear selected items, and a Button for canceling the selection process. Note that we are going to implement this application using Java language. A sample video is given below to get an idea about what we are going to do in this article.
Step by Step Implementation
Step 1: Creating a new project
- Open a new project.
- We will be working on Empty Activity with language as Java. Leave all other options unchanged.
- You can change the name of the project at your convenience.
- There will be two default files named activity_main.xml and MainActivity.java.
If you don’t know how to create a new project in Android Studio then you can refer to How to Create/Start a New Project in Android Studio?
Step 2: Working on activity_main.xml file
Here we will design the user interface of our application. We will be using the following components for their respective works:
- TextView: To allow users to click on it so that a dropdown list can appear and display the selected items in it.
- Drop-down arrow: to indicate to the user that some action will be completed after clicking it.
Use the following code in the activity_main.xml file.
XML
<?xml version="1.0" encoding="utf-8"?>
<!-- Relative layout as parent layout-->
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity">
<!-- text view to display selected items-->
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@android:drawable/editbox_background"
android:drawableRight="@android:drawable/arrow_down_float"
android:drawablePadding="16dp"
android:hint="Select Language"
android:padding="12dp" />
</RelativeLayout>
After executing the above code design of the activity_main.xml file looks like this.
Step 3: Working with MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Java
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.Collections;
public class MainActivity extends AppCompatActivity {
// initialize variables
TextView textView;
boolean[] selectedLanguage;
ArrayList<Integer> langList = new ArrayList<>();
String[] langArray = {"Java", "C++", "Kotlin", "C", "Python", "Javascript"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// assign variable
textView = findViewById(R.id.textView);
// initialize selected language array
selectedLanguage = new boolean[langArray.length];
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Initialize alert dialog
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
// set title
builder.setTitle("Select Language");
// set dialog non cancelable
builder.setCancelable(false);
builder.setMultiChoiceItems(langArray, selectedLanguage, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i, boolean b) {
// check condition
if (b) {
// when checkbox selected
// Add position in lang list
langList.add(i);
// Sort array list
Collections.sort(langList);
} else {
// when checkbox unselected
// Remove position from langList
langList.remove(Integer.valueOf(i));
}
}
});
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// Initialize string builder
StringBuilder stringBuilder = new StringBuilder();
// use for loop
for (int j = 0; j < langList.size(); j++) {
// concat array value
stringBuilder.append(langArray[langList.get(j)]);
// check condition
if (j != langList.size() - 1) {
// When j value not equal
// to lang list size - 1
// add comma
stringBuilder.append(", ");
}
}
// set text on textView
textView.setText(stringBuilder.toString());
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// dismiss dialog
dialogInterface.dismiss();
}
});
builder.setNeutralButton("Clear All", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// use for loop
for (int j = 0; j < selectedLanguage.length; j++) {
// remove all selection
selectedLanguage[j] = false;
// clear language list
langList.clear();
// clear text view value
textView.setText("");
}
}
});
// show dialog
builder.show();
}
});
}
}
Output:
Similar Reads
How to Implement Country Code Picker in Android?
Country Code Picker (CCP) is an android library that helps users to select country codes (country phone codes) for telephonic forms. CCP provided a UI component that helps the user to select country codes, country flags, and many more in an android spinner. It gives well-designed looks to forms on t
3 min read
How to Implement Polling in Android?
Many times you may have seen on some apps like youtube, LinkedIn, etc. polling is done and users choose their options whatever they want to choose. Here we are going to implement polling in Android Studio. What we are going to build in this article? In this article, we will ask the user a question a
4 min read
How to Implement Date Range Picker in Android?
Date Range Picker is a widely used feature in many popular Android apps and an essential component of Material Design. It allows users to select a range of dates such as a start and end date for various purposes including scheduling, filtering data, and setting time boundaries. Some Of The Real Life
4 min read
How to Implement Item Click Interface in Android?
When we click on an item in an application either it gives some information or it redirects the user to any other page. In this article, we will learn that how we can implement Item Click Interface in an android application. What we are going to build in this article?In this article, we will be usin
5 min read
How to Implement Custom Searchable Spinner in Android?
Android Spinner is a view similar to the dropdown list which is used to select one option from the list of options. It provides an easy way to select one item from the list of items and it shows a dropdown list of all values when we click on it. The default value of the android spinner will be the c
5 min read
How to Select an Image from Gallery in Android?
Selecting an image from a gallery in Android is required when the user has to upload or set their image as a profile picture or the user wants to send a pic to the other. So in this article, it's been discussed step by step how to select an image from the gallery and preview the selected image. Have
4 min read
How to Use Phone Selector API in Android?
Phone Selector API is used to detect phone numbers being used in the phone. Using this you can avoid manual input of Phone Numbers by users and prompt them to choose the desired number. A sample image is given below to get an idea about what we are going to do in this article. Note that we are going
3 min read
How to Implement Android SearchView with Example
The SearchView widget is used to provide a search interface to the user so that the user can enter his search query and submit a request to the search provider and get a list of query suggestions or results.Class Syntax:public class SearchView extends LinearLayout implements CollapsibleActionViewCla
4 min read
How to Delete Multiple RecyclerView Items in Android?
RecyclerView is an advanced version of ListView with improved performance. When you have a long list of items to show you can use RecyclerView. It has the ability to reuse its views. In RecyclerView when the View goes out of the screen or is not visible to the user it wonât destroy it, it will reuse
8 min read
DropDownView in Android
DropDownView is another exciting feature used in most Android applications. It is a unique way of representing the menu and other options in animated form. We can get to see the list of options under one heading in DropDownView. In this article, we are going to see how to implement DropDownView in A
4 min read