Android: Autocompletetextview Show Suggestions When No Text Entered

Android: AutoCompleteTextView show suggestions when no text entered

This is documented behavior:

When threshold is less than or equals 0, a threshold of 1 is
applied.

You can manually show the drop-down via showDropDown(), so perhaps you can arrange to show it when you want. Or, subclass AutoCompleteTextView and override enoughToFilter(), returning true all of time.

Show AutoCompleteTextView on the first click

Just call autoCompleteTextView.showDropDown() in onTouch method.

mAnswer.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
mAnswer.showDropDown();
}

return false;
}
});

Show all items in AutocompleteTextView without writing text

You need to extend AutoCompleteTextView,

"When threshold is less than or equals 0, a threshold of 1 is
applied.".

setThreshold

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

public class InstantAutoComplete extends AutoCompleteTextView {

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

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

public InstantAutoComplete(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 && getFilter()!=null) {
performFiltering(getText(), 0);
}
}

}

in xml

<AutoCompleteTextView ... /> to <your.namespace.InstantAutoComplete ... />

EDIT 1

Create new class named InstantAutoComplete then put this code into the class.

In your layout xml use this class like

then find this widget in your actity (onCreate method).

Look at this example

AutoCompleteTextView - Show suggestions after selection

write below code in your AutoCompleteTextView.setOnItemClickListener()

autoComplete.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
autoComplete.postDelayed(new Runnable() {
@Override
public void run() {
autoComplete.showDropDown();
}
},100);
autoComplete.setText(autoComplete.getText().toString());
autoComplete.setSelection(autoComplete.getText().length());

}
});

And that's it, it will work like charm!!!

This will give you hint for your question, change according to your need and adapter data

AutoCompleteTextView force to show all items

Basically, after 5-6 hours of experimentation to understand how the damn filter works, I wrote my own adapter which does exactly what I want:

    public class burtuAdapteris extends ArrayAdapter<String> implements Filterable {

ArrayList<String> _items = new ArrayList<String>();
ArrayList<String> orig = new ArrayList<String>();

public burtuAdapteris(Context context, int resource, ArrayList<String> items) {
super(context, resource, items);

for (int i = 0; i < items.size(); i++) {
orig.add(items.get(i));
}
}

@Override
public int getCount() {
if (_items != null)
return _items.size();
else
return 0;
}

@Override
public String getItem(int arg0) {
return _items.get(arg0);
}

@Override

public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {

if(constraint != null)
Log.d("Constraints", constraint.toString());
FilterResults oReturn = new FilterResults();

/* if (orig == null){
for (int i = 0; i < items.size(); i++) {
orig.add(items.get(i));
}
}*/
String temp;
int counters = 0;
if (constraint != null){

_items.clear();
if (orig != null && orig.size() > 0) {
for(int i=0; i<orig.size(); i++)
{
temp = orig.get(i).toUpperCase();

if(temp.startsWith(constraint.toString().toUpperCase()))
{

_items.add(orig.get(i));
counters++;

}
}
}
Log.d("REsult size:" , String.valueOf(_items.size()));
if(!counters)
{
_items.clear();
_items = orig;
}
oReturn.values = _items;
oReturn.count = _items.size();
}
return oReturn;
}

@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if(results != null && results.count > 0) {
notifyDataSetChanged();
}
else {
notifyDataSetInvalidated();
}

}

};

return filter;

}

}

And it's simple to use, just replace original adapter with this:

final burtuAdapteris fAdapter = new burtuAdapteris(this, android.R.layout.simple_dropdown_item_1line, liste);

In my case liste is: ArrayList<String> liste = new ArrayList<String>();

AutoCompleteTextview doesn't show dropdown suggestion for first character entry

I worked around this problem by replacing the text with the same text in the edittext a moment after the adapter is created:

public void replaceAllText() {

Timer timer = new Timer();
final long DELAY = 300; // milliseconds

timer.cancel();
timer = new Timer();
timer.schedule(
new TimerTask() {
@Override
public void run() {
((Activity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
String replace = searchFriends.getText().toString();
searchFriends.setText(replace);
searchFriends.setSelection(searchFriends.getText().length());
}
});
}
},
DELAY
);
}


Related Topics



Leave a reply



Submit