Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customized the Suggestion List of AutoComplete Text View

This is the code for my AutoCompleteTextView :

    String[] countries = getResources().getStringArray(R.array.countries_array);
    final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.item_list ,countries);
    textView = (AutoCompleteTextView) dialog.findViewById(R.id.autoCompleteTextView1);
    adapter.setNotifyOnChange(true);
          textView.setAdapter(adapter);

This is item_list :

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp"
android:textColor="#000"
 android:drawableRight="@drawable/arrow">

and this is the output :

enter image description here

I want to change this pop-up kind of suggestion box , and want to show the suggestion list a little bit below from the AutoCompleteTextView ..similar to this : (please disregard the apple design, it’s just about where the results appear)

enter image description here

How can i do this ..Please suggest . Thanks.!!

like image 732
Nibha Jain Avatar asked Dec 13 '11 10:12

Nibha Jain


People also ask

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.

How do you get the ID of the selected item of the AutoCompleteTextView in Android?

OnItemClickListener and set onItemClickListener() on the AutoCompleteTextView to get the user selected item value from the list. Notice that while using the ArrayAdapter , we have provided a layout object as argument android. R.

What does Android completion hint attribute in AutoCompleteTextView?

This defines the hint view displayed in the drop down menu. This defines the number of characters that the user must type before completion suggestions are displayed in a drop down menu. This is the View to anchor the auto-complete dropdown to.


2 Answers

Use the android:dropDownAnchor, android:dropDownHorizontalOffset, android:dropDownVerticalOffset and maybe others on the AutoCompleteTextView in your layout. See AutocompleteTextView.

like image 53
Anasthase Avatar answered Oct 21 '22 05:10

Anasthase


I used a ListView below the AutoCompleteTextView. I added my custom adapter to both the ListView and the AutoCompleteTextView. I the set the height and width of the AutoCompleteTextView dropdown to 0:

myListView.setAdapter(myCustomAdapter);
myAutoComplete.setAdapter(myCustomAdapter);
myAutoComplete.setDropDownHeight(0);
myAutoComplete.setDropDownWidth(0);

Since the listview is also attached to the same adapter it'll get updated when you update the autocomplete.

like image 44
Kabeer Avatar answered Oct 21 '22 06:10

Kabeer