Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android AutoCompleteTextView popup moving after displaying

When I use the autocompletetextview everything works fine except it keeps switching between two positions: the correct one right below the textview and quite a ways lower. It starts wrong, but almost immediately moves to the correct position. However this is very annoying when typing or backspacing as it happens for every letter. I am using android studio.

It appears as if two events are simultaneously trying to decide the layout. Sometimes it will stick in one position or the other.

**I slowed down the filtering process with a custom adapter and it looks like when text is entered it moves into the incorrect position, and then when the filtering is done it moves back into the correct position.

Incorrect

Incorrect

Correct:

Correct

java (in OnCreate())-

String[] drugs = new String[]{"Nexium","Amoxicillin","LEVOCETIRIZINE DIHYDROCHLORIDE", "Advil", "Advair Diskus", "Daraprim"};

    AutoCompleteTextView drugNameAutoComplete = ((AutoCompleteTextView) findViewById(R.id.drugNameEditText));
    drugNameAutoComplete.setAnimation(null);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,drugs);
    drugNameAutoComplete.setAdapter(adapter);

And the layout code-

<AutoCompleteTextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/drugNameEditText"
            android:enabled="true"
            android:singleLine="true"
            android:layout_below="@+id/lookingForView"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:dropDownVerticalOffset="50dp"
            android:hint="@string/drug_name" />

If I remove the dropDownVeticalOffset I get flickering between the correct value and this-

enter image description here

like image 819
ghostbust555 Avatar asked Feb 13 '16 21:02

ghostbust555


2 Answers

To change this position use dropDownAnchor attribute and reference another view id. (view under autocomplete)

android:dropDownAnchor

View to anchor the auto-complete dropdown to. If not specified, the text view itself is used.

and you have many attributes for dropdown..

You better to use implement Filter to autocomplete adapter and pass the entered text from OnQueryTextListener of autocomplete, and set the selected text by calling adapter.getitem.

like image 179
SreeAndroidDev Avatar answered Sep 19 '22 18:09

SreeAndroidDev


Change your xml to-

<AutoCompleteTextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/drugNameEditText"
    android:enabled="true"
    android:singleLine="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    **android:dropDownVerticalOffset="0dp"**
    **android:dropDownAnchor="@id/drugNameEditText"**
    android:hint="drug" />
like image 42
Vaibhav Sharma Avatar answered Sep 19 '22 18:09

Vaibhav Sharma