androidkotlinviewadapterautocompletetextview

AutoCompleteTextView not adjusting height


I have an autoCompleteTextView with a custom array adapter attached but when i filter results as the user types the view of the list seems to have a fixed size.

see here

and here

in the first example I have 3 matching results and in the second 1. As you can notice at the bottom of the screenShot the view of the list has a fixed size. I tried to calculate on the fly the height of the size of the results like below, but it wont work

private fun adjustListHeight() {
        // After calling adapter.notifyDataSetChanged()
        val filteredItemCount = autoCompleteAdapter.getFilteredItemCount()

        // Calculate the height of the dropdown list based on the number of items and the height of the first item
        val firstItemView = autoCompleteAdapter.getView(0, null, binding.descriptionTextInputLayout)
        val measureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
        firstItemView.measure(measureSpec, measureSpec)
        val itemHeight = firstItemView.measuredHeight

        val verticalPadding = firstItemView.paddingTop + firstItemView.paddingBottom

        val dropdownHeight = if (filteredItemCount > 0) {
            // Calculate the total height of the dropdown list based on the number of items and item height
            val totalHeight = (filteredItemCount * itemHeight) + (2 * verticalPadding)
            totalHeight
        } else {
            // Default height when no items are available
            ViewGroup.LayoutParams.WRAP_CONTENT
        }

        // Set the calculated height to the AutoCompleteTextView's dropdown list
        binding.descriptionEditText.dropDownHeight = dropdownHeight
    }

I call the method above right after the notifyDataChanged() in my adapter.

Also this is the view in xml

<AutoCompleteTextView
                android:id="@+id/descriptionEditText"
                style="@style/TextAppearance.FontPath.Regular"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:cursorVisible="true"
                android:ellipsize="end"
                android:inputType="textCapCharacters"
                android:maxLines="1"
                android:dropDownHeight="wrap_content"
                android:paddingTop="8dp"
                android:textColor="@color/global_gray_dark_text"
                android:textCursorDrawable="@drawable/drawable_cursor_color"
                android:textSize="20sp"
                android:theme="@style/customCursor"
                tools:text="69774839234234234234234234234"
                tools:drawableEnd="@drawable/ic_contacts_2"/>

I need the view of the list to adjust to the filtered items height


Solution

  • For anyone interested the answer lies here. Both of the solutions are working

    AutoCompleteTextView dropdown height not wrapping to content