ghostbust555
ghostbust555

Reputation: 2049

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

Upvotes: 13

Views: 6846

Answers (5)

David Delgado
David Delgado

Reputation: 1

I had the same problem and too many hours later I've found the solution.

You should change:

android:layout_width="wrap_content" to android:layout_width="match_parent"

I hope it works for you too.

Upvotes: 0

puargs
puargs

Reputation: 352

I had the same problem on my end with the newest API for some reason. I solved the problem by explicitly setting dropDownAnchor, dropDownVerticalOffset, and dropDownHeight in both the XML and the onTextChanged event.

Edit: After some checking, it seems this behavior is INCONSISTENT BETWEEN APIs starting with Marshmallow (API 23). Try changing your code to the following:

final float scale = context.getResources().getDisplayMetrics().density;
int verticalOffsetDP;
int dropDownHeightDP = (int) (200 * scale + 0.5f);

int currentApiVersion = android.os.Build.VERSION.SDK_INT;

if (currentApiVersion < Build.VERSION_CODES.M)
{
    // Prior to Marshmallow DropDownVerticalOffset works as expected
    verticalOffset = 0;
}
else
{
    // Marshmallow has some weird DropDownVerticalOffset behavior so we have to compensate
    verticalOffset = (int) (50 * scale + 0.5f);
}

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);

drugNameAutoComplete.setDropDownAnchor(R.id.drugNameEditText);
drugNameAutoComplete.setDropDownVerticalOffset(verticalOffsetDP);
drugNameAutoComplete.setDropDownHeight(dropDownHeightDP);

And your XML to the following:

<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:dropDownAnchor="@id/drugNameEditText"
        android:dropDownHeight="200dp"
        android:hint="@string/drug_name" />

Note that you have to calculate DP based on screen dimensions in code. I got the code example from the excellent accepted response in this thread: Android and setting width and height programmatically in dp units

The reason you have to set dropDownHeight as well is because otherwise Android has a behavior to push it up to the top of the screen if the list extends beyond the height boundaries of the view, further complicating the moving-around issues. If you actually don't mind this (some people don't, depends on your view setup), you can remove dropDownHeight from both programmatic and XML.

I should note that these changes still don't make things EXACTLY the same between Marshmallow and other APIs. Looks like there is still a few pixels difference. But it gets pretty close.

Upvotes: 1

Stephen Edwards
Stephen Edwards

Reputation: 69

Did you by any chance use the same id on the other AutoCompleteTextView? I can reproduce the behaviour you describe by including two views with the same id.

Upvotes: 2

Vaibhav Sharma
Vaibhav Sharma

Reputation: 2319

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" />

Upvotes: 2

SreeAndroidDev
SreeAndroidDev

Reputation: 141

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.

Upvotes: 4

Related Questions