Skip to content

[AutoCompleteTextView] Pop up menu calls adapter.getView(), not adapter.getDropDownView() #4374

Open
@nikclayton

Description

@nikclayton

Description:

When an adapter is set on an AutoCompleteTextView, the adapter's getView() method is called to get the view to show in the dropdown / popup menu. From the adapter documentation (e.g., on ArrayAdapter) I had expected getDropViewView() to be called.

Expected behavior:

getDropDownView() to be called. It is documented as:

Gets a View that displays in the drop down popup the data at the specified position in the data set.

where as getView() is documented as:

Get a View that displays the data at the specified position in the data set.

Since I'm using AutoCompleteTextView in an exposed dropdown menu (https://github.com/material-components/material-components-android/blob/master/docs/components/Menu.md#exposed-dropdown-menu-example) I expected the method with dropdown in the name to be called.

It's probably too late to change the code (outside of a breaking change release), but a very clear note in the documentation everywhere it demonstrates setting an adapter that getDropDownView is ignored would be helpful.

Source code:

val items = listOf("Item 1", "Item 2", "Item 3", "Item 4")
val adapter = object : ArrayAdapter(requireContext(), android.R.layout.simple_list_item_1, items) {
    init {
        setDropDownViewResource(android.R.layout.simple_list_item_2)
    }

    override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup): View {
        // bind data to the UI here, populating the second textview in layout.simple_list_item_2
    }
}
(textField.editText as? AutoCompleteTextView)?.setAdapter(adapter)

Instrumenting getDropDownView and getView with logging calls quickly shows that only getView is called.

Android API version: 31

Material Library version: 1.12.0

Device: Emulator

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions