Exposed Drop-Down Menu in Android
The Exposed Drop-Down menu is the replacement for Spinner in Android because Spinner is not that customizable like the new exposed Drop-Down menu. Below is the sample GIF to give an idea of what we are going to build. Note that we are going to implement this project using the Kotlin language.
Step by Step Implementation
Step 1: Create a new Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.
Step 2: Working with the activity_main.xml file
Go to the activity_main.xml file and refer to the following code. Below is the code for the activity_main.xml file.
XML
<? xml version = "1.0" encoding = "utf-8" ?> < androidx.constraintlayout.widget.ConstraintLayout android:layout_width = "match_parent" android:layout_height = "match_parent" tools:context = ".MainActivity" > <!--Create a TextInputLayout and choose the style, for now we are choosing the OutlinedBox ExposedDropdownMenu --> < com.google.android.material.textfield.TextInputLayout style = "@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_margin = "20dp" app:layout_constraintLeft_toLeftOf = "parent" app:layout_constraintRight_toRightOf = "parent" app:layout_constraintTop_toTopOf = "parent" > <!-- Inside text input layout add an auto complete text view and make its input type to none--> < AutoCompleteTextView android:id = "@+id/autoCompleteTextView" android:layout_width = "match_parent" android:layout_height = "match_parent" android:layout_weight = "1" android:inputType = "none" android:text = "Choose Programming language" /> </ com.google.android.material.textfield.TextInputLayout > </ androidx.constraintlayout.widget.ConstraintLayout > |
Step 3: Add string array items to the string.xml file. We will use this data to inflate the drop-down items
XML
< string-array name = "programming_languages" > < item >Java</ item > < item >Kotlin</ item > < item >Python</ item > < item >CPP</ item > </ string-array > |
Step 4: Create a new layout file and name it a dropdown_item.xml file
Go to the dropdown_item.xml file and refer to the following code. Below is the code for the dropdown_item.xml file. It is the single-text view which we will use as a single item of the dropdown.
XML
< TextView android:id = "@+id/textView" android:layout_width = "match_parent" android:textColor = "@color/black" android:textStyle = "bold" android:padding = "14dp" android:layout_height = "wrap_content" android:text = "TextView" /> |
Step 5: Working with MainActivity.kt
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.widget.ArrayAdapter import android.widget.AutoCompleteTextView class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super .onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get reference to the string array that we just created val languages = resources.getStringArray(R.array.programming_languages) // create an array adapter and pass the required parameter // in our case pass the context, drop down layout , and array. val arrayAdapter = ArrayAdapter( this , R.layout.dropdown_menu, languages) // get reference to the autocomplete text view val autocompleteTV = findViewById<AutoCompleteTextView>(R.id.autoCompleteTextView) // set adapter to the autocomplete tv to the arrayAdapter autocompleteTV.setAdapter(arrayAdapter) } } |
Output:
Please Login to comment...