In this article we will see a simple method to customize AutoCompleteTextView so that it can list images along with text when user input some characters in it. We will use SimpleAdapter to store both images and text which is displayed in the AutoCompeteTextView.
This application is developed in Eclipse Classic ( 4.2.0 ) with ADT plugin ( 20.0.3 ) and Android SDK ( R20.0.3 )
1. Create a new Android application project namely “AutoCompleteTextViewDemo”
2. Design application launcher icon
3. Create a blank activity
4. Enter MainActivity Details
5. Remove Android Support Library if exists
By default Eclipse ( 4.2.0) adds Android Support Library to an Android application project. For this application, we don’t need to use this support library. So the library file libs/android-support-v4.jar may be removed manually via ProjectExplorer by simply right click on the file and then clicking the menu item “delete”.
6. Create a new folder namely drawable under the folder res
7. Download and extract the given below zip file to the drawable folder

8. Update the file res/values/strings.xml
<resources> <string name="app_name">AutoCompleteTextViewDemo</string> <string name="hello_world">Hello world!</string> <string name="menu_settings">Settings</string> <string name="title_activity_main">AutoCompleteTextViewDemo</string> <string name="str_hnt_autocomplete">Enter Country Name</string> </resources>
9. Create a class namely CustomAutoCompleteTextView in the file src/in/wptrafficanalyzer/autocompletetextviewdemo/CustomAutoCompleteTextView.java to customize AutoCompleteTextView
package in.wptrafficanalyzer.autocompletetextviewdemo; import java.util.HashMap; import android.content.Context; import android.util.AttributeSet; import android.widget.AutoCompleteTextView; /** Customizing AutoCompleteTextView to return Country Name * corresponding to the selected item */ public class CustomAutoCompleteTextView extends AutoCompleteTextView { public CustomAutoCompleteTextView(Context context, AttributeSet attrs) { super(context, attrs); } /** Returns the country name corresponding to the selected item */ @Override protected CharSequence convertSelectionToString(Object selectedItem) { /** Each item in the autocompetetextview suggestion list is a hashmap object */ HashMap<String, String> hm = (HashMap<String, String>) selectedItem; return hm.get("txt"); } }
10. Update the layout file res/layout/activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <in.wptrafficanalyzer.autocompletetextviewdemo.CustomAutoCompleteTextView android:id="@+id/autocomplete" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:textColor="@android:color/black" android:hint="@string/str_hnt_autocomplete" android:completionThreshold="1" /> <TextView android:id="@+id/tv_currency" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_below="@id/autocomplete" /> </RelativeLayout>
11. Create a layout file for CustomAutoCompleteTextView in the file res/layout/autocomplete_layout.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <ImageView android:id="@+id/flag" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@string/hello_world" android:padding="10dp" /> <TextView android:id="@+id/txt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="15dp" android:padding="10dp" /> </LinearLayout>
12. Update the class MainActivity in the file src/in/wptrafficanalyzer/autocompletetextviewdemo/MainActivity.java
package in.wptrafficanalyzer.autocompletetextviewdemo; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.SimpleAdapter; import android.widget.TextView; public class MainActivity extends Activity { // Array of strings storing country names String[] countries = new String[] { "India", "Pakistan", "Sri Lanka", "China", "Bangladesh", "Nepal", "Afghanistan", "North Korea", "South Korea", "Japan" }; // Array of integers points to images stored in /res/drawable-ldpi/ int[] flags = new int[]{ R.drawable.india, R.drawable.pakistan, R.drawable.srilanka, R.drawable.china, R.drawable.bangladesh, R.drawable.nepal, R.drawable.afghanistan, R.drawable.nkorea, R.drawable.skorea, R.drawable.japan }; // Array of strings to store currencies String[] currency = new String[]{ "Indian Rupee", "Pakistani Rupee", "Sri Lankan Rupee", "Renminbi", "Bangladeshi Taka", "Nepalese Rupee", "Afghani", "North Korean Won", "South Korean Won", "Japanese Yen" }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Each row in the list stores country name, currency and flag List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>(); for(int i=0;i<10;i++){ HashMap<String, String> hm = new HashMap<String,String>(); hm.put("txt", countries[i]); hm.put("flag", Integer.toString(flags[i]) ); hm.put("cur", currency[i]); aList.add(hm); } // Keys used in Hashmap String[] from = { "flag","txt"}; // Ids of views in listview_layout int[] to = { R.id.flag,R.id.txt}; // Instantiating an adapter to store each items // R.layout.listview_layout defines the layout of each item SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.autocomplete_layout, from, to); // Getting a reference to CustomAutoCompleteTextView of activity_main.xml layout file CustomAutoCompleteTextView autoComplete = ( CustomAutoCompleteTextView) findViewById(R.id.autocomplete); /** Defining an itemclick event listener for the autocompletetextview */ OnItemClickListener itemClickListener = new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) { /** Each item in the adapter is a HashMap object. * So this statement creates the currently clicked hashmap object * */ HashMap<String, String> hm = (HashMap<String, String>) arg0.getAdapter().getItem(position); /** Getting a reference to the TextView of the layout file activity_main to set Currency */ TextView tvCurrency = (TextView) findViewById(R.id.tv_currency) ; /** Getting currency from the HashMap and setting it to the textview */ tvCurrency.setText("Currency : " + hm.get("cur")); } }; /** Setting the itemclick event listener */ autoComplete.setOnItemClickListener(itemClickListener); /** Setting the adapter to the listView */ autoComplete.setAdapter(adapter); } /** A callback method, which is executed when this activity is about to be killed * This is used to save the current state of the activity * ( eg : Configuration changes : portrait -> landscape ) */ @Override protected void onSaveInstanceState(Bundle outState) { TextView tvCurrency = (TextView) findViewById(R.id.tv_currency) ; outState.putString("currency", tvCurrency.getText().toString()); super.onSaveInstanceState(outState); } /** A callback method, which is executed when the activity is recreated * ( eg : Configuration changes : portrait -> landscape ) */ @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { TextView tvCurrency = (TextView) findViewById(R.id.tv_currency) ; tvCurrency.setText(savedInstanceState.getString("currency")); super.onRestoreInstanceState(savedInstanceState); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
13. Screenshots of the application in execution

Figure 6 : Displays the currency corresponding to the country selected from the autocompletetextview
14. Download

15. Reference
http://developer.android.com/guide/index.html

I am George Mathew, working as software architect and Android app developer at wptrafficanalyzer.in
You can hire me on hourly basis or on project basis for Android applications development.
For hiring me, please mail your requirements to info@wptrafficanalyzer.in.
My other blogs
store4js.blogspot.com
just Awesome you rock
good tutorial.
i took this and wanted to do the dropdown list more complex.
i wanted to make it the following form:
——————-
| |____________|
| | |
——————-
as u can c, its a linearlayout of type horizontal where the first view in it is an image and the second view is another linearlayout of type vertical that consist of two textviews.
with this kind of layout, the autocomplete dropdown list doesnt work
have any idea ?
How can you change the font of the textview?
Thank you very much! You’ve helped me a lot!!
I tried to use custom bitmap in your example but it didnot work. How can it be achieved. the bitmaps are stored in arraylist.
Its Good tutorial, but I am adding two text in each row instead Image + Text , at that time that will show single record as twice in suggestion box. If i just put “S” then it show Sri Lanka two times in suggestion. How can we handle this ?? Do you have any idea.
hi how to set autocomplete text hint name scrolling or marquee
Thanks, George
By the looks of it this is precisely what we need for our Android app. Specifically,
– Replace default FormData file with a custom file (names, eg ‘Godzilla’)
– Access custom file for autosuggest when the user types into form
– Preferably, the ability to update custom file from Server
We’ve got a good dev for the core App; we could give this to him or to a specialist. Do you take such work on? Pls get in touch if you do. Thanks again,
Dee.
Great tutorial man, million thanks
In my case i am fetching data from database for Hashmap and for some item in dropdown its showing duplicate entries. Can you tell me Why this might happen ?
sir when i have entered “Sri ” or “Sri Lanka” its show no suggestion but there should be a suggestion ….please solve it
hi there nice tutorial but i added two button into ur code, not working OnItemClickListener pls can u help me any mechanism
Gracias por el aporte, me fue de mucha utilidad de forma didáctica.
Tutorial is good .but how can i add space after some text like
if i am searching Sri lanka then drop down hides after sri +space
.how tell me some tricks
Hi,
I wanted to display image from remote url for this exact example. Can you guide me how to do that?
Because I am fetching data from JSON API.
thanks
Waqas
Great tutorial.
How to setImageBitmap from a url rather than use an Array of integers points to images stored in /res/drawable-ldpi/?
how can v restrict the autocomplete in listview for example : if at 1st row we enter sri lanka and at the 2nd row we try to add same sri lanka it should restrict. please reply soon