I want to show a list of the last 'n' entries when the user taps on a text input. How can I do it?
Right now I am extending EditText, incorporating the 'x' button to clear its content. So, my idea is incorporate this functionality to this widget
I want to show a list of the last 'n' entries when the user taps on a text input. How can I do it?
Right now I am extending EditText, incorporating the 'x' button to clear its content. So, my idea is incorporate this functionality to this widget
The comment of @pskink put me on the correct way
First, the AutoCompleteTextView has to be reimplemented
public class QuickSayAutoComplete extends AutoCompleteTextView {
private Context ctx;
public QuickSayAutoComplete(Context context) {
super(context);
ctx = context;
init();
}
public QuickSayAutoComplete(Context context, AttributeSet attrs) {
super(context, attrs);
ctx = context;
init();
}
public QuickSayAutoComplete(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
ctx = context;
init();
}
To add the last elements introduced, a ClickListener has to be added.
this.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
String text = getText().toString();
// The text is not empty
if ( (!text.matches("")) && (text.length() > 0)) {
// The last element introduced will be the first
ArrayAdapter<String> adapter = (ArrayAdapter<String>) getAdapter();
adapter.remove(text);
adapter.insert(text, 0);
}
}
});
The ACTV's adapter has to be an ArrayList, if not, we cannot remove elements
String[] array = {"Hello", "Good Morning", "Nice to see you"};
ArrayList<String> greetings = new ArrayList<String>();
greetings.addAll(Arrays.asList(array));
ArrayAdapter<String> adapter = new ArrayAdapter<String>(ctx, android.R.layout.simple_dropdown_item_1line, greetings);
setAdapter(adapter);
setThreshold(0);
Finally, the DropDown list is shown (with all the entries) when the user touch the ACTV and there is not text input yet. As the user introduces an input, the list gets shorter. It could be perfect if we can limit the number of entries shown in this point
// TouchListener to show the list without user input
setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
showDropDown();
return false;
}
});