AutocompleteTextView suggestion list goes up

情到浓时终转凉″ 提交于 2019-12-18 12:13:17

问题


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

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.


回答1:


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.




回答2:


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




回答3:


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.




回答4:


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.




回答5:


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>



回答6:


use: android:dropDownHeight="wrap_content" in AutoCompleteTextView




回答7:


Set Full Layout containing Autocompletetextview inside Scrollview

This will solve your problem!



来源:https://stackoverflow.com/questions/11554766/autocompletetextview-suggestion-list-goes-up

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!