I have implemented AutoCompleteTextView
as follows:
MainActivity.java
...
public static String[] myData=new String[]{"Africa (AF)","America (AFM)","Apple (AMP)"};
text=(AutoCompleteTextView)v.findViewById(R.id.first_state);
ArrayAdapter adapter = new ArrayAdapter(getActivity(),R.layout.autocompletetextview_row,R.id.textViewItem,myData);
text.setAdapter(adapter);
text.setThreshold(1);
text.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
selected_station = (String)parent.getItemAtPosition(position);
//TODO Do something with the selected text
}
});
In layout of AutoCompleteTextView
:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:padding="10dp" >
<TextView
android:id="@+id/textViewItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Item name here..."
android:textColor="#000000"
android:textSize="20sp" />
</RelativeLayout>
When I type like "af..", it shows me Africa (AF) to select but not America (AFM).
The data is just an example data. it's not that I am using AutoCompleteTextView
for just 3 items.
EDIT: when I remove the parentheses, it works properly. But I need to keep the parentheses for further use.
The default implementation of the filter for ArrayAdapter
is searching the beginning of words (separated by space), I mean it uses startsWith
. You will need to implement an ArrayFilter
which uses contains
together with startsWith
.
Your issue will be solved by the following steps:
ArrayAdapter.java
file from hereCustomArrayAdapter.java
, for example).In the file, you will find a private class ArrayFilter
. Then, add valueText.contains(prefixString)
and words[k].contains(prefixString)
as the following:
if (valueText.startsWith(prefixString) || valueText.contains(prefixString)) {
newValues.add(value);
} else {
final String[] words = valueText.split(" ");
final int wordCount = words.length;
// Start at index 0, in case valueText starts with space(s)
for (int k = 0; k < wordCount; k++) {
if (words[k].startsWith(prefixString) || words[k].contains(prefixString)) {
newValues.add(value);
break;
}
}
}
Use that customized ArrayAdapter
for your AutoCompleteTextView
And here is the result screenshot:
Hope this helps!