0

I need to create dropdown menu with search functionality. My goal is to allow user to easily pick from quite large list of predefined values. I found TextInputLayout with AutocompleteTextView as very nice and simple.

But I am missing 2 functions: a) I don't want to allow user to fill custom value. He must pick from predefined values only. It can be done by adding android:inputType="none" to AutoCompleteTextView, but after that, search functionality is gone. b) It would be nice to be able to search values containing users input, not only these that starts with it. Example: write "land" and get "Poland".

Is it possible to achieve this functionality with AutocompleteTextView? Or is there any simple alternative?

Thank you.

This is my implementation of AutocompleteTextView:

XAML

<com.google.android.material.textfield.TextInputLayout
    style="@style/Widget.Material3.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Country">

    <AutoCompleteTextView
        android:id="@+id/actCountries"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </AutoCompleteTextView>
</com.google.android.material.textfield.TextInputLayout>

Fragment

val countries = arrayListOf<String>("Czech Republic","Slovakia", "Poland", "Germany","Austria")
val countriesAdapter = ArrayAdapter<String>(requireContext(), R.layout.support_simple_spinner_dropdown_item, countries)
binding?.actCountries?.setAdapter(countriesAdapter )

1 Answer 1

0

An alternative approach can be using a TextWatcher. You can update the dropdown list inside the onTextChanged callback by using .filter extension on the countries list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.