Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show a drop down list of recently entered text when clicks on an android edit box

I have to show a drop down list of recently typed texts when the user edits the text.Example is the login pages shows previously logged in users

like image 475
user987362 Avatar asked Jan 16 '23 17:01

user987362


2 Answers

You are looking for the AutoCompleteTextView http://developer.android.com/reference/android/widget/AutoCompleteTextView.html

Create a list of logins

When a user logs in, you need to save that login to some sort of persistent storage (database, text file).

Creating a auto complete list

Everytime you create the form with the EditText login

  1. Extract the previous login values from the database
  2. Create an String array out of those previous login values
  3. Create an array adapter out of the String array
  4. Attach the array adapter to your AutoCompleteTextView.
like image 122
Gophermofur Avatar answered Jan 19 '23 07:01

Gophermofur


I just implemented this after not being able to find a solution. I did what Gopher said, but store it as private preferences instead of in a file. I also added an if statement to stop the item from being on the list twice.

public void YOURSUBMITBUTTON(View view) 
{
    PrevItemsAutoComplete customitemname = (PrevItemsAutoComplete) findViewById(R.id.YOURTEXTVIEW);

    SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
    editor.putString("customitemname",customitemname.getText().toString());
    editor.commit();

    customitemname.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, updatedropdown(5)));
}

public String[] updatedropdown(int listlength)
{
    boolean itemalreadyinlist=false;
    SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();

    for(int i = 0; i<listlength; i++)
    {
        if (getPreferences(MODE_PRIVATE).getString("customitemname","").equals(getPreferences(MODE_PRIVATE).getString("recentlistitem"+String.valueOf(i),"")))
        {
            itemalreadyinlist=true;
            for(int j = i; j>0; j--)
            {
                editor.putString("recentlistitem"+String.valueOf(j),getPreferences(MODE_PRIVATE).getString("recentlistitem"+String.valueOf(j-1),""));
            }
            editor.putString("recentlistitem0",getPreferences(MODE_PRIVATE).getString("customitemname",""));
            editor.commit();
            break;
        }
    }

    if( !itemalreadyinlist)
    {
        for(int i = listlength-1; i>0; i--)
        {
            editor.putString("recentlistitem"+String.valueOf(i),getPreferences(MODE_PRIVATE).getString("recentlistitem"+String.valueOf(i-1),""));
        }
        editor.putString("recentlistitem0",getPreferences(MODE_PRIVATE).getString("customitemname",""));
        editor.commit();
    }

    String[] recentlist = new String[listlength];
    for(int i=0;i<listlength;i++)
    {
        recentlist[i] = getPreferences(MODE_PRIVATE).getString("recentlistitem"+String.valueOf(i),"");
    }

    return recentlist;
}

where customitemname is your textview containing the input. This above code is invoked when you click a button.

If you are wondering what PrevItemsAutoComplete is, it is a custom class extending AutoCompleteTextView. While it works with the latter, I prefer the former. Here is the class:

import android.content.Context;  
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.AutoCompleteTextView;

public class PrevItemsAutoComplete extends AutoCompleteTextView {

public PrevItemsAutoComplete(Context context) {
    super(context);
}

public PrevItemsAutoComplete(Context arg0, AttributeSet arg1) {
    super(arg0, arg1);
}

public PrevItemsAutoComplete(Context arg0, AttributeSet arg1, int arg2) {
    super(arg0, arg1, arg2);
}

@Override
public boolean enoughToFilter() {
    return true;
}

@Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
    super.onFocusChanged(focused, direction, previouslyFocusedRect);
    if (focused){
        setText("");
        performFiltering("", 0);
        showDropDown();
    }
}

}

The PrevItemsAutoComplete was modded from the original here: https://stackoverflow.com/a/5783983/2066079.

Just remember to use <your.namespace.PrevItemsAutoComplete ... /> instead of <AutoCompleteTextView ... /> in the xml if you choose to use this class.

Also, you might have to add customitemname.addTextChangedListener(this); to your onCreate.


I spent a lot of time on this so please don't forget to upvote if this helped you.

like image 20
dberm22 Avatar answered Jan 19 '23 08:01

dberm22