Material design has an Exposed Dropdown Menu implemented with a AutoCompleteTextView
like this:
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_text">
<AutoCompleteTextView
android:id="@+id/filled_exposed_dropdown"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.google.android.material.textfield.TextInputLayout>
But I need to have the same look an feel but with the spinner behavior (Without autocomplete, just display popup and select an item).
I disabled editing on AutoCompleteTextView to avoid to use the auto complete, but after selecting one of the item then the autocomplete just list the items that matches the item text selected given a filter that is used in this view. This is the code:
<AutoCompleteTextView
android:id="@+id/filled_exposed_dropdown"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
**android:editable="false"**/>
Also I put a listener to open items list when click on it
val editTextFilledExposedDropdown = findViewById<AutoCompleteTextView>(R.id.filled_exposed_dropdown)
editTextFilledExposedDropdown.setAdapter(adapter)
editTextFilledExposedDropdown.setOnClickListener {
(it as AutoCompleteTextView).showDropDown()
}
So, I would want to know if it is possible to implement this but with a spinner
This was my attempt using a spinner, but it not display the style correctly OutlinedBox.Dense.ExposedDropdownMenu
and also display two arrow bottom icon, I think one for the style and another for the spinner.
this was the code with the spinner:
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Hint">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/layout_id"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense.ExposedDropdownMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Currency">
<androidx.appcompat.widget.AppCompatSpinner
android:id="@+id/my_spinner"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</com.google.android.material.textfield.TextInputLayout>
This is what i was looking for, just disable autocomplete functionality of AutoCompleteTextView by overriding getFilter method. the answer is posted here (AutoCompleteTextView - disable filtering)
Update:
Can be achieved with 1 line of code:
android:editable="false"
On the AutoCompleteTextView
From the material.io docs:
Note: In order to have a non editable variation of the menu, you should disable user input in the AutoCompleteTextView. That can be achieved by setting android:editable="false" on the AutoCompleteTextView.
No idea why Google chose to use a deprecated attribute for this though...
tools:ignore="Deprecated"
can be used though to remove the warning
You can use android:inputType="none"
on the AutoCompleteTextView
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With