Scrolling drop-down-menu over the keyboard in autocompletetextview

自古美人都是妖i 提交于 2019-11-30 17:35:18
Deepak Goel

Just add android:dropDownHeight="100dp" to the AutoCompleteTextView tag in your layout file, it will work.

Hoang

You can also use android:dropDownAnchor="@id/ to anchor the dropdown to a view.

pgarriga

Let me explain my little trick to avoid that the "drop-down" displays behind the keyboard. The trick is with the dropDownAnchor property. The solution is set the anchor with a view located on the top of the screen, so the menu will leave from that position, and therefore, will not be covered by the keyboard. For example:

android:dropDownAnchor="@+id/topview"

I know that is an ugly solution but this control is too limited.

You need to do two things. First, adjust the soft input mode of that activity in the manifest.

android:windowSoftInputMode="stateHidden|adjustResize"

This ensures views are laid out again when the keyboard is shown. Then, set a global layout listener in your oncreate on the top level view to do the dropdown height calculation when the layout changes. Adjust the dropdown height to be the height of everything below the keyboard, minus some padding if you want.

v.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

        @Override
        public void onGlobalLayout() {
                autoCompleteView.setDropDownHeight(view2.getHeight());
}

Where view2 is the view/layout that includes everything below the autocompleteview.

Just add getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); to your fragment or activity

A simple solution that works perfectly with all resolutions is to use the android:dropDownAnchor property with a resource ID that references your activity toolbar.

<my.app.ContactAutoCompleteTextView
                            android:id="@+id/autocomplete_textview"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:gravity="top"
                            android:dropDownAnchor="@id/appbar"
                            android:inputType="text|textMultiLine|textCapSentences|textAutoCorrect"
                            android:paddingBottom="12dp"
                            android:textColor="@color/text_primary"
                            android:textColorLink="@color/secondary"
                            android:textSize="@dimen/text_medium" />
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!