El menú desplegable expuesto es el reemplazo de Spinner en Android porque Spinner no es tan personalizable como el nuevo menú desplegable expuesto. A continuación se muestra el GIF de muestra para dar una idea de lo que vamos a construir. Tenga en cuenta que vamos a implementar este proyecto utilizando el lenguaje Kotlin.
XML
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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" 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>
XML
<string-array name="programming_languages"> <item>Java</item> <item>Kotlin</item> <item>Python</item> <item>CPP</item> </string-array>
XML
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 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" />
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) } }