24

possible duplicates Android : autocompletetextview, suggestion list displays above the textview?

I am fully trying to display suggestion list overlapping on keyboard when suggestion list scroll by user but it always open up side.

here I am getting this way

enter image description here

here is my manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sl"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".SuggestionListActivity" 
            android:windowSoftInputMode="adjustResize|adjustPan|stateHidden">
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

here is my main.xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" android:padding="10dp">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" 
        android:layout_margin="10dp"/>

    <TextView android:layout_margin="10dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="This is testing for the auto complete textview in this application to display suggestion list overlapping on keyboard." />

    <AutoCompleteTextView android:id="@+id/autocomplete"
            android:layout_width="fill_parent" android:layout_margin="5dp"
            android:layout_height="wrap_content" android:hint="Search"
            android:layout_marginLeft="5dp" android:dropDownHeight="300dp"
            android:inputType="textAutoComplete" android:singleLine="true"
            />

</LinearLayout>

what to do in this code to display the suggestion over keyboard when list was focus.

Community
  • 1
  • 1
Pratik
  • 30,639
  • 18
  • 84
  • 159
  • i have a doubt that are you entering any text in the AutoCompleteTextView ? because there is no data but list is displayed!! – Ram kiran Pachigolla Jul 19 '12 at 06:09
  • 2
    Why do you want it to be on the softkeyboard?...it wont allow user to type more characters right....like if there are 10000 strings with "he..." and only 5 strings with "hez..." better allow user to type "z" instead of scrolling 10000 items...when i was using autocomplete in my app i got the same problem, but i think this is the best approach considering the low screen size devices – Archie.bpgc Jul 19 '12 at 06:10
  • if you want to display suggestions with out entering any data means when onclick on the AutoCompleteTextView, then disable the soft keyboard and implement on click event for the AutoCompleteTextView – Ram kiran Pachigolla Jul 19 '12 at 06:11
  • I have requirement that when user click/tap on autocomplete then display the list with all data having fix size so user can select from the list, if user can type no problem it care itself to display list which are containing that charater – Pratik Jul 19 '12 at 06:14
  • 1
    In Gingerbread phones and before the popup window is calculated depending on the available space. So if there is more space available at top then the popup will show above. But Post Gingerbread this has been fixed to show the autocomplete suggestion only at the bottom. So the same app will work correctly on ICS phones. – nandeesh Aug 14 '12 at 14:21
  • have you got answer of above ? – Mahesh Suthar Apr 06 '16 at 09:20
  • any fix for this thread ? – ralphgabb Oct 20 '16 at 07:55

8 Answers8

12

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.

Mike T
  • 4,747
  • 4
  • 32
  • 52
  • 4
    @MaheshSuthar: Showing code won't help you. The point is that there needs to be more space below the dropdown than above it. So, however you achieve that is up to you. – Mike T Apr 06 '16 at 13:00
7

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

Pratik
  • 30,639
  • 18
  • 84
  • 159
Gautam
  • 7,868
  • 12
  • 64
  • 105
  • I already set this 300dp for dropDownHeight and it'll goes always upward and also display list when it focus by this focuschangelistener. The problem is for small device. – Pratik Aug 16 '12 at 06:24
  • Did you try putting it in a scroll view and moving the `AutoCompleteTextview` near the top ? – Gautam Aug 16 '12 at 06:50
  • actually I have put this in tab so on top of tab are available and that much space is there so I have test this to place this control with some top margin – Pratik Aug 16 '12 at 07:14
7

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.

Petter Hesselberg
  • 5,062
  • 2
  • 24
  • 42
2

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.

bshears
  • 1,119
  • 9
  • 18
1

use: android:dropDownHeight="wrap_content" in AutoCompleteTextView

Ankit Saini
  • 342
  • 3
  • 11
0

Just adding the android:dropDownHeight="100dp" to the AutoCompleteTextView tag in your layout file will be the best solution I guess! it will simply control the height of drop down hight and allow us to scroll!

<AutoCompleteTextView
        android:id="@+id/acetxt_assignclient"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"   
        android:dropDownHeight="100dp">
</AutoCompleteTextView>
Alireza Noorali
  • 3,129
  • 2
  • 33
  • 80
LOG_TAG
  • 19,894
  • 12
  • 72
  • 105
0

I have found if you are using a nested scroll view, it is more prone to open the view above or below as it sees fit where as when you are using a regular scroll view it opens below.

-2

Set Full Layout containing Autocompletetextview inside Scrollview

This will solve your problem!

arpan
  • 1