13

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

1
  • Try to specify a height?
    – iWumbo
    Feb 22, 2016 at 23:31

5 Answers 5

4

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.

1
  • I tried this and nothing. I'm not sure what filtering has to do with the layout though. Feb 14, 2016 at 6:54
2
+25

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" />
1
  • 1
    This did not help. These are the defaults as well so I don't see how they would have an effect? Feb 22, 2016 at 5:30
2

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.

1
  • Nope :( just checked and then renamed it just to be sure. Feb 22, 2016 at 21:28
1

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.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.