Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Exposed dropdown menus]: Layout expands to width of menu content, not the width of the text that will be displayed. #4375

Open
nikclayton opened this issue Nov 14, 2024 · 0 comments

Comments

@nikclayton
Copy link

Description: When using an adapter to provide more complex content to an exposed drop down menu, the width of the TextInputLayout is expanded to the width of the menu, not the width of the longest content that will be shown in TextInputLayout.

This is probably easier to explain with screenshots.

This is the screenshot of an exposed drop down menu with three items, "Show", "Warn", and "Hide", set using setOptions().

This is what I expect.

However, I want to show the user additional information about each choice. The labels are deliberately short because there isn't much screen real estate available. So I have a trivial adapter that uses a two-line view, like this:

    data class FilterDescription(val title: String, val descr: String) {
        override fun toString() = title
    }

    val adapter = object : ArrayAdapter<FilterDescription>(
        requireContext(),
            android.R.layout.simple_list_item_2,
            android.R.id.text1,
        ) {
            val items = listOf( /* FilterDescription objects here */ )

            init {
                addAll(items)
            }

            override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
                val view = super.getView(position, convertView, parent)
                val text1 = view.findViewById<TextView>(android.R.id.text1)
                val text2 = view.findViewById<TextView>(android.R.id.text2)
                getItem(position)?.let { item ->
                    text1.text = item.title
                    text2.text = item.descr
                }
                return view
            }
        }

If I set this adapter the menu displays two lines per item as expected. However, the width of the TextInputLayout is now the width of the longest descr. Like this with the menu open:

and this with the menu closed:

Expected behavior: The layout should be the maximum width of the content that will be shown in it, not the maximum width of the pop up menu.

Source code: As above.

Android API version: 31

Material Library version: 1.12.0

Device: Emulator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants