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
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With