Search code examples
androidautocompletetextview

Android custom layout for AutoCompleteTextView


I may be missing something simple here, but I have an AutoCompleteTextView that contains some very long items. When one is clicked, it shows the text correctly in the EditText and spans it over several lines.

However, I want it to be in multiple lines on the popup as well so that the users can see which item they are selecting.

Here is my custom layout:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:ellipsize="none"
    android:maxLines="100"
    android:scrollHorizontally="false" />

Here is my initialisation of the array and adapter:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                R.layout.dropdown_item_wrap_line,
                getResources().getStringArray(R.array.building_descriptions));

mBuildingDesc.setAdapter(adapter);

Solution

  • My guess is that your AutoCompleteTextView is limited to singleLine, so using a custom layout with default TextView should work neatly.

    You will need to make a new Layout and name it custom_item.xml, then do it like this...

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    
        <TextView 
            android:id="@+id/autoCompleteItem"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:textSize="14sp"    
            />
    
    </LinearLayout>
    

    Then while using Adapter on AutoCompleteTextView do

    AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete);
    ArrayAdapter<String> adapter  = new ArrayAdapter<String>(this, R.layout.custom_item, R.id.autoCompleteItem, StringArray);
    textView.setAdapter(adapter);