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
When I now select the "longitem" item the width is increased:
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)