Soft keyboard hides half of EditText

28,796

Solution 1

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

Solution 2

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

Solution 3

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:

Result:

Solution 4

Disable the keyboard on window startup

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

Solution 5

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.

Share:
28,796

Related videos on Youtube

Gintas_
Author by

Gintas_

Updated on March 19, 2021

Comments

  • Gintas_
    Gintas_ about 3 years

    I have a ListView and the last list item contains EditText:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <RelativeLayout
            android:id="@+id/contentLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="30dp"
            android:orientation="vertical" >
    
            <ImageView
                android:id="@+id/test"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignRight="@+id/messageEditText"
                android:layout_below="@+id/messageEditText"
                android:layout_marginRight="14dp"
                android:src="@drawable/test" />
    
            <EditText
                android:id="@+id/messageEditText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:layout_toLeftOf="@+id/sendImageButton"
                android:ems="10"
                android:gravity="top"
                android:hint="@string/messageEditText"
                android:inputType="textMultiLine"
                android:minHeight="55dp"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:paddingTop="10dp" />
    
            <ImageButton
                android:id="@+id/sendImageButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@id/messageEditText"
                android:layout_alignParentRight="true"
                android:layout_alignTop="@id/messageEditText"
                android:adjustViewBounds="true"
                android:maxHeight="55dp"
                android:maxWidth="55dp"
                android:padding="12dp"
                android:scaleType="centerInside"
                android:src="@drawable/sendmessage" />
    
        </RelativeLayout>
    </RelativeLayout>
    

    Half of the EditText is hidden. I also can't scroll ListView. Any solution?

    image

    • Lamorak
      Lamorak over 8 years
    • dazza5000
      dazza5000 over 7 years
      I'm experiencing the same issue event after setting android:windowSoftInputMode="adjustResize|stateHidden" and using a scrollview. Any other suggestions?
    • Ashlesha Sharma
      Ashlesha Sharma about 7 years
      [The property android:windowSoftInputMode="adjustResize" doesn't work with full screen activity, you have to remove activity full screen flags in order to get advantage of adjust resize. ](stackoverflow.com/a/42204350/3926264)
  • IgorGanapolsky
    IgorGanapolsky almost 9 years
    @Tanis.7x You cannot use both - adjustPan and adjustResize.
  • Bryan Herbst
    Bryan Herbst almost 9 years
    I never said to use both- I mentioned both because either could be applicable depending on the desired end behavior. You should look at the documentation for both and choose the one that provides the behavior you want.
  • sham.y
    sham.y almost 9 years
    Hi @IgorGanapolsky.. stateHidden setting will hide soft keyboard when user enters new Activity (even if EditText control gains the focus). Soft keyboard will be shown only when user clicks the edit box control.