Exposed Dropdown Menu not showing items
Asked Answered
A

3

6

Exposed Dropdown Menu doesn't show items after user selection and fragment transition.

Following is the basic xml declaration:

<com.google.android.material.textfield.TextInputLayout
    ...
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
 
         <AutoCompleteTextView
            .... 
            android:id="@+id/dropdown"
            android:dropDownHeight="300dp"
            android:inputType="none" />
</com.google.android.material.textfield.TextInputLayout>

And, the declared code on the fragment (inside onViewCreated()):

    val items = listOf("Material", "Design", "Components", "Android")
    val adapter = ArrayAdapter(requireContext(), R.layout.item_menu, items)
    dropdown.setAdapter(adapter)
    dropdown.setText(items[0], false)

As mentioned here, it was set on AutoCompleteTextView's setText method (dropdown.setText("", false)) the filter parameter as false. However, after navigating to a next fragment and coming back to it only the pre-selected text is shown on the dropdown.

Fragments are changed using navigation component (v. 2.3.2).

Ale answered 29/1, 2021 at 18:15 Comment(1)
I meet the same problem.Graiggrail
T
1

I had the same problem. I searched for issues on github page. I found this https://github.com/material-components/material-components-android/issues/2012#issuecomment-808853621 work around for now. It works.

  1. Create an extension like below

    fun AutoCompleteTextView.showDropdown(adapter: ArrayAdapter<String>?) {
     if(!TextUtils.isEmpty(this.text.toString())){
         adapter?.filter?.filter(null)
     }
    }
    

Then on click of dropdown

 binding.quaters.setOnClickListener {
            binding.quaters.showDropdown(arrayAdapter)
        }

That's all it should work. This seems to be a bug which should be fixed hopefully.

Tshombe answered 23/11, 2021 at 11:12 Comment(0)
S
0

The fragment's view gets destroyed when using the navigation component. (maybe not always, but it will certainly happen some of the time as you experienced)

I think you might be able to make it work simply by adding a condition:

if (savedInstanceState == null) {
    dropdown.setText(items[0], false)
}

So that the default is only set when not restoring the view state.

Otherwise it's just a matter saving the state as usual. Here's a documentation article about it if you're unsure what I'm talking about. It will essentially amount to adding the following code to your fragment:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    val item = savedInstanceState?.getInt("selectedPos", 0) ?: 0
    dropdown.setText(items[item], false)
}

override fun onSaveInstanceState(outState: Bundle) {
    super.onSaveInstanceState(outState)
    outState.putInt("selectedPos", dropdown.getListSelection())
}

If you're using the MVVM architecture, you can save the selected position using SavedStateHandle in your ViewModel, when it gets changed.

Shererd answered 29/1, 2021 at 18:36 Comment(2)
Unfortunately, none of the solutions shown in your code work.Ale
Then I misunderstood your problem. Nevertheless you must save the state appropriately, because the navigation component destroys fragments as they are navigated out of, losing view state.Shererd
O
0

This is a temprorary solution that is working for me - https://github.com/material-components/material-components-android/issues/2012#issuecomment-868181589

Write the setup code for ExposedDropdownMenu in onResume() of a fragment, instead of onCreateView()/onViewCreated()

    override fun onResume() {
        super.onResume()

        val sortingArtist = resources.getStringArray(R.array.sortingArtist)
        val arrayAdapterArtist = ArrayAdapter(requireContext(), R.layout.dropdown_items_artist, sortingArtist)
        binding?.autoCompleteTextViewArtist?.setAdapter(arrayAdapterArtist)
        binding?.autoCompleteTextViewArtist?.setText(sortingArtist[0], false)

    }

For reference - https://material.io/components/menus/android#exposed-dropdown-menus

Oilcloth answered 28/2, 2022 at 9:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.