Soft keyboard hides half of EditText

安稳与你 提交于 2019-11-28 07:27:51

Solution was to set an android:softInputMode attribute to adjustResize in Manifest and put layout(not list item layout) inside a ScrollView.

sham.y

I Had a similar issue, then i used android:windowSoftInputMode="adjustResize|stateHidden" as mentioned here.. It works very well for me..

From my early days as a Android Developer I struggled with the virtual keyboard. I am amazed that Android is still not giving an elegant and clear solution for this.

So here is the Workaround that will put this mess behind you. it will work without the ScrollView workaround or giving away your full screen flag.

  1. Add this awesome Library to your gradle file:

compile'net.yslibrary.keyboardvisibilityevent:keyboardvisibilityevent:1.0.1'

  1. Make sure your activity has the following keyboard settings:

android:windowSoftInputMode="adjustResize"

  1. wrap your EditText with a vertical LinearLayout and add a View with Visibility of Gone:

    <com.ylimitapp.ylimitadmin.views.NormalFontEditText
        android:id="@+id/input_et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textCapSentences|textMultiLine"
        android:imeOptions="actionSend"
        android:paddingStart="20dp"
        android:paddingEnd="40dp"
        android:paddingTop="10dp"
        android:maxHeight="120dp"
        android:adjustViewBounds= "true"
        android:scrollHorizontally="false"
        android:textColorHint="#7b7b7b"
        android:hint="@string/type_your_message"
        android:background="@drawable/msg_inputfield_bg"
        android:textColor="@color/black_text_color"
        android:textSize="15.33sp"
        />
    
        <View
            android:id="@+id/keyboard_view"
            android:layout_width="match_parent"
            android:layout_height="20dp"
            android:visibility="gone"
            />
    
    </LinearLayout>
    
  2. calculate the screen size so you can calculate the height of the view that pushes the EditText:

    private void storeScreenHeightForKeyboardHeightCalculations() {
                Rect r = new Rect();
                View rootview = getActivity().getWindow().getDecorView(); 
                rootview.getWindowVisibleDisplayFrame(r);
                mOriginalScreenHeight = r.height();
    
                Rect rectangle = new Rect();
                Window window = getActivity().getWindow();
                window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
                int statusBarHeight = rectangle.top;
                int contentViewTop =
                        window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
                int titleBarHeight= contentViewTop - statusBarHeight;
    
                if (titleBarHeight == 0) {
                    mOriginalScreenHeight -= (24 * Utils.getDensity(getContext()));
                }
            }
    
    1. Add a Listener for keyboard open and close event and then set the height of the View below the EditText on runtime so we will set the height properly on any device and custom keyboards. then simply make it Visible when the Keyboard is Open:
private void addkeyBoardlistener() {
           KeyboardVisibilityEvent.setEventListener(
                            getActivity(),
                            new KeyboardVisibilityEventListener() {
                                @Override
                                public void onVisibilityChanged(boolean isOpen) {
                                    if (isOpen) {
                                        Rect r = new Rect();
                                        View rootview = getActivity().getWindow().getDecorView(); // this = activity
                                        rootview.getWindowVisibleDisplayFrame(r);
                                        int keyboardHeight = (mOriginalScreenHeight - r.height());
                                        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) keyboard_view.getLayoutParams();
                                        params.height = (int) ((keyboardHeight + 5 * Utils.getDensity(getContext())));
                                        keyboard_view.setLayoutParams(params);
                                        keyboard_view.setVisibility(View.VISIBLE);
                                    } else {
                                        keyboard_view.setVisibility(View.GONE);
                                    }
                                }
                            });
                }

This is the result:

Disable the keyboard on window startup

this .getWindow().setSoftInputMode(WindowManager.LayoutParams. SOFT_INPUT_STATE_ALWAYS_HIDDEN );

Set an android:softInputMode attribute for your Activity element in your Manifest.

See the Manifest documentation for a full list of valid values and their effect. The ones of particular interest to you will likely be adjustPan and adjustResize.

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