0

I have an AutoCompleteTextView (as part of an Exposed Dropdownmenu). I don't want to set a fixed width to it, instead the width shall be determined by the widest item from the adapter. So I've set the width to wrap_content, but this changes every time another item is selected.

Example: I currently have two items:

"short" (shown by default)
"longitem"

This renders as

enter image description here

When I now select the "longitem" item the width is increased:

enter image description here

Well, I understand that this happens. But it's very annoying. Instead of all these size adaptions, I want the AutoCompleteTextView to permanently (no matter which item is selected) have the size it has when "longitem" is selected. How can I do this?

Used code (as on the official exposed dropdown documentation)

dropdown_menu_popup_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
android:padding="16dp"
android:ellipsize="end"
android:maxLines="1"
android:textAppearance="?attr/textAppearanceSubtitle1"
tools:text="some item (tool text)" />

layout file:

<AutoCompleteTextView
    android:id="@+id/exposed_dropdown"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:editable="false"
    android:text="short"
    />

Adapter (Kotlin)

val myValues = arrayOf("short", "longitem")

val adapter = ArrayAdapter<String>(
    requireContext(), 
    R.layout.dropdown_menu_popup_item,
    myValues
)

binding.exposedDropdown.setAdapter(adapter)
stefan.at.wpf
  • 13,061
  • 31
  • 116
  • 199
  • 1
    use match_parent – ThavaSelvan Feb 10 '20 at 11:50
  • 1
    It is because of `android:layout_width="wrap_content"` To set static width use `match_parent` or actual value – qki Feb 10 '20 at 11:53
  • Or if match_parent is too wide use a fixed width XXXdp. I don't know of any way which check the widest child item and matches the width to that. – just_user Feb 10 '20 at 11:54
  • Thank for your replies, but match_parent won't work. The AutoCompleteTextview (or better said the wrapping TextInputLayout) is part of a constraint layout where it is in one row with another item. Settings the AutoCompleteTextView to match_parent makes it span the width of the whole screen, pushing away the other item. I only want the AutoCompleteTextView to be as wide as necessary! match_parent makes it wider than needed. – stefan.at.wpf Feb 10 '20 at 11:54
  • @just_user yeah, how I can get the widest width to set it explicitely is exactly what I need to know ): – stefan.at.wpf Feb 10 '20 at 11:55
  • Just a quick thought, create a custom AutoCompleteTextView and measure the width of mPopup and set the width depending on that value!?!?! – just_user Feb 10 '20 at 12:00
  • with mPopup, do you mean the width of the dropdown menu? that initially is only as large as the item displayed first. The views for the dropdown menu are built by the ArrayAdapter, so it should somehow be handled at that stage. Just wondering if there is not default functionality for that or what's the best way. – stefan.at.wpf Feb 10 '20 at 12:04
  • You're probably right there. Another way could be to measure the text with of the longest item, use that value with with the paddings and the drawableRight width to create a fixed value for the width. Just considering that feels messy! ;) – just_user Feb 10 '20 at 12:31
  • too complex for me, added bug report: https://github.com/material-components/material-components-android/issues/1001 if meanwhile anyone has an idea for a workaround, I'd be highly interested \: – stefan.at.wpf Feb 10 '20 at 14:19

0 Answers0