Android AutoComplete EditText with history

By: Nicklas Møller Jepsen


In this post I'm going to show you have to get an Android app to have some autocomplete/history functionality. You can achieve this by using the AutoCompleteEditText.

The Auto Complete EditText

We will use the AutoCompleteEditText which is made just for this purpose.

However, we need a place to store the user's input and for that we are using SharedPreferences.

It will be wrapped in a Activity, ready for you to try out!


First we're going to declare the view in the actovity_main.xml add the following:

    android:hint="Enter something"/>

The go to your MainActivity.java and we're going to setup the methods and variables we need to store the history in the SharedPreferences. Create a couple of class variables:

public static final String PREFS_NAME = "PingBusPrefs";
public static final String PREFS_SEARCH_HISTORY = "SearchHistory";
private SharedPreferences settings;
private Set<String> history;

These should explain them selves; it is just some strings used as keys for the preferences and the the settings variable and a set to contain the actual data of the history/input from the user.

Now we add a method to load create an adapter used by our AutoCompletEditText to show recent history:

private void setAutoCompleteSource()
    AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.textInput);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(
		this, android.R.layout.simple_list_item_1, history.toArray(new String[history.size()]));

Then we add a method we can call to add a history item:

private void addSearchInput(String input)
    if (!history.contains(input))

Last but not least, we're going to implement the save method, so that we can save the preferences:

private void savePrefs()
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    SharedPreferences.Editor editor = settings.edit();
    editor.putStringSet(PREFS_SEARCH_HISTORY, history);


Now that we have these methods, we're going to initialize everything in the onCreate method. First add these 3 lines:

settings = getSharedPreferences(PREFS_NAME, 0);
history = settings.getStringSet(PREFS_SEARCH_HISTORY, new HashSet<String>());


This will initilize the settings and read the history from the settings. Then we're going to wire up the onKey event on the AutoCompleteEditText and write the implementation to to listen for "Enter" key presses:

// Set the "Enter" event on the search input
final AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.textInput);
textView.setOnKeyListener(new View.OnKeyListener()
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        // If the event is a key-down event on the "enter" button
        if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
                (keyCode == KeyEvent.KEYCODE_ENTER)) {

            return true;
        return false;

Now the last thing we need, is to call the savePrefs method in the onStop method which will then make sure that anychanges made to the history will be copied to the settings and the settings will be save:

protected void onStop()