AutocompleteTextView suggestion list goes up
I've had this problem before. For me, there was more screen space above the AutocompleteTextView
than below (testing on a "normal" device), so the list opened upwards. I adjusted my layout slightly so that there was more space below the AutocompleteTextView
and it started opening downwards. That's what fixed it for me.
The trick is to ensure that the desired drop-down height is never larger than the available space below. My approach is to create a subclass that overrides showDropDown
:
public class DownOnlyAutoCompleteTextView extends AppCompatAutoCompleteTextView {
private final static int MINIMAL_HEIGHT = 50;
public DownOnlyAutoCompleteTextView(Context context) {
super(context);
}
public DownOnlyAutoCompleteTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public DownOnlyAutoCompleteTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void showDropDown() {
Rect displayFrame = new Rect();
getWindowVisibleDisplayFrame(displayFrame);
int[] locationOnScreen = new int[2];
getLocationOnScreen(locationOnScreen);
int bottom = locationOnScreen[1] + getHeight();
int availableHeightBelow = displayFrame.bottom - bottom;
if (availableHeightBelow >= MINIMAL_HEIGHT) {
setDropDownHeight(availableHeightBelow);
}
super.showDropDown();
}
}
Then use this in your layout, e.g.:
<your.package.DownOnlyAutoCompleteTextView
android:id="@+id/auto_complete_text_view"
android:layout_margin="12dp"
android:hint="AutoComplete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="HardcodedText" />
Adjust MINIMAL_HEIGHT
to fit your requirements -- if there's no or very little space below, it's probably better not to force the issue.
EDIT
As mentioned in the comments, passing a negative number to setDropDownHeight
will trigger an exception in some Android versions. As long as you define a MINIMAL_HEIGHT
greater than zero, that should not be a problem.
You can either adjust the layout so that there is more space below the AutoCompleteTextView
or
you can change the dropdown height android:dropDownHeight
and set some high value,
this would work when its inside a scrollView
and the AutoCompleteTextView
is near the top.
To display the list of options on focus do something like this
autoCompleteTextView.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus) {
autoCompleteTextView.showDropDown();
}
}
});
This would display a list of options when the user focuses on the AutoCompleteTextView
Here's my solution
private final static int DELAY_MS = 500;
autoCompletionTextView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
autoCompletionTextView.requestFocus();
new Handler().postDelayed(() -> autoCompletionTextView.showDropDown(), DELAY_MS);
return false;
}
});
After keyboard shows up suggestion list is listed above yout AutoCompletionTextView.