Show A Drop Down List Of Recently Entered Text When Clicks On An Android Edit Box
Solution 1:
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
- Extract the previous login values from the database
- Create an String array out of those previous login values
- Create an array adapter out of the String array
- Attach the array adapter to your AutoCompleteTextView.
Solution 2:
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.
publicvoid 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)));
}
publicString[] 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 = newString[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;
publicclassPrevItemsAutoCompleteextendsAutoCompleteTextView {
publicPrevItemsAutoComplete(Context context) {
super(context);
}
publicPrevItemsAutoComplete(Context arg0, AttributeSet arg1) {
super(arg0, arg1);
}
publicPrevItemsAutoComplete(Context arg0, AttributeSet arg1, int arg2) {
super(arg0, arg1, arg2);
}
@OverridepublicbooleanenoughToFilter() {
returntrue;
}
@OverrideprotectedvoidonFocusChanged(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.
Solution 3:
Use the AutocompleteTextView
Solution 4:
you can use AutoComplete TextView.But you need to make change Like you have to maintain ArrayList of previously Log User.
Post a Comment for "Show A Drop Down List Of Recently Entered Text When Clicks On An Android Edit Box"