23

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

Gintas_
  • 4,940
  • 12
  • 44
  • 87
  • possible duplicate of [Android soft keyboard covers edittext field](http://stackoverflow.com/questions/3295672/android-soft-keyboard-covers-edittext-field) – Lamorak Aug 19 '15 at 17:03
  • 1
    I'm experiencing the same issue event after setting android:windowSoftInputMode="adjustResize|stateHidden" and using a scrollview. Any other suggestions? – dazza5000 Aug 31 '16 at 21:49
  • [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. ](http://stackoverflow.com/a/42204350/3926264) – Ashlesha Sharma Feb 13 '17 at 12:46

6 Answers6

33

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

Gintas_
  • 4,940
  • 12
  • 44
  • 87
9

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

Community
  • 1
  • 1
shyam.y
  • 1,441
  • 1
  • 16
  • 17
  • 4
    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. – shyam.y Jul 11 '15 at 09:18
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:

Gal Rom
  • 6,221
  • 3
  • 41
  • 33
2

Disable the keyboard on window startup

this .getWindow().setSoftInputMode(WindowManager.LayoutParams. SOFT_INPUT_STATE_ALWAYS_HIDDEN );
it's me
  • 188
  • 1
  • 1
  • 10
1

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.

Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120
  • 1
    @Tanis.7x You cannot use both - `adjustPan` and `adjustResize`. – IgorGanapolsky Jul 09 '15 at 16:54
  • 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. – Bryan Herbst Jul 09 '15 at 17:02
0

Add this in the manifest like so:

<activity
    android:name="your_activity_name"
    android:windowSoftInputMode="adjustResize"
    android:theme="@style/Theme.AppCompat.DayNight.NoActionBar" />

Then add paddingBottom="20dp" in your layout and editText.

cela
  • 2,352
  • 3
  • 21
  • 43