androidautocompletetextview

Android AutoCompleteTextView only works for the first word?


I'm trying to use the autocomplete text and I'm running into a small problem.

It's working fine when I try to type the first word as one of the countries, but if I try to type any thing else and then one of the countries nothing happens, like: "ger" (will giv me Germany) and: "hello ger" (will not give Germany)

The same thing happens if I just click enter once and start in line two, it only works for the first word and first line. Also, if I do use it for the first word and try to use it again, like Germany (from autocomplete) and then type ger, nothing will pop.

Any idea how to make it work for the rest of the text or how to control it (set it on or of after a selected or place in line?

Code:

 AutoCompleteTextView et1;
    private static final String[] COUNTRIES = new String[] {
            "Belgium", "France", "Italy", "Germany", "Spain"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        et1=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, COUNTRIES);
        et1.setAdapter(adapter);
        et1.setThreshold(0);
    }

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main3"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.asaf1.workersreports.Main3Activity">

    <AutoCompleteTextView
        android:text=""
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/autoCompleteTextView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:textAlignment="viewEnd"
        android:gravity="right|top"/>
</RelativeLayout>

Solution

  • You have to create custom AutoCompleteTextView. In Adapter implement the Filter class and and override performFilterring() method. In this method you have to write some code which matches the result. In your case you have to check for input string contains the one of your list of strings.

    Check this links where custom Adapter is used: Custom AutoCompleteTextView behavior and AutoCompleteTextView backed by CursorLoader