Dropdown size adjust according to displayed results

Viewed 18

I use autoComplete in my project. Here is the usage of autoComplete:

In Code:

    val arrayAdapter = context?.let {
        ArrayAdapter<String>(
            it, // Context
            android.R.layout.simple_list_item_1,
            autocompleteOptions // Array
        )
    }

    autoCompleteTextView.setAdapter(arrayAdapter)

    // Set an item click listener for auto complete text view
    autoCompleteTextView.onItemClickListener =
        AdapterView.OnItemClickListener { parent, _, position, _ ->
           
           //some logic
        }

In XML:

            <AutoCompleteTextView
                android:id="@+id/actvSearch"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true""
                android:textAppearance="?android:attr/textAppearanceListItemSmall"
                android:completionThreshold="0"
                android:dropDownHeight="200dp"
                android:layout_gravity="center_vertical" />

My question is there any way to adjust the size of the dropdown list according to the current options displayed in the dropdown?

1 Answers

You would need to set it manually in code, based on the number of entries in your arrayAdapter:

autoCompleteTextView.setDropDownHeight(arrayAdapter.getCount() * rowHeight)

where you either calculate each row height, or just have it pre-set as a static height

Related