Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
like image 863
Carrie Hall Avatar asked Mar 26 '13 16:03

Carrie Hall


People also ask

How do I create an AutoCompleteTextView field in XML?

In android, we can create an AutoCompleteTextView control in two ways either manually in an XML file or create it in the Activity file programmatically. First we create a new project by following the below steps: Click on File, then New => New Project. After that include the Kotlin support and click on next.

What is Android Completionhint attribute in AutoCompleteTextView?

Defines the hint view displayed in the drop down menu. Defines the number of characters that the user must type before completion suggestions are displayed in a drop down menu.

How do I set autocomplete text on Android?

If you want to get suggestions , when you type in an editable text field , you can do this via AutoCompleteTextView. It provides suggestions automatically when the user is typing. The list of suggestions is displayed in a drop down menu from which the user can choose an item to replace the content of the edit box with.


1 Answers

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);
like image 57
Arif Nadeem Avatar answered Oct 05 '22 08:10

Arif Nadeem