Search code examples
androidkeyboardwindow-soft-input-mode

Soft Keyboard pushed button up above it. How to fix it?


I have ConstraintLayout with ScorllView and Button below it (attached to bottom of the screen. When I am editing EditText input inside ScrollView. Then appearing keyboard is moving my ScrollView content up (desired behaviour, so I can scroll to the end of it) but it also pushing button up (undesired behaviour).

I think I can change windowAdjustMode, maybe I could detect keyboard showing and then hide this button? But this two solutions aren't perfect. enter image description here

XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<ScrollView
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintBottom_toTopOf="@id/submitButton"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_margin="0dp">
    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

       <EditText /> goes here 
    </android.support.constraint.ConstraintLayout>
</ScrollView>
    <Button
        android:id="@+id/submitButton"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_margin="0dp"
        android:text="@string/wizard_singup_step_submit_button"
        style="@style/FormSubmitButton"

        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />
</android.support.constraint.ConstraintLayout>

Solution

  • This might help, haven't tried it myself, try adding the below code to your activity tag inside your manifest

    Edit - added stateHidden to achieve what you're looking for, the button will be at the bottom and the elements inside the scroll view can be scrolled.

    android:windowSoftInputMode="adjustPan|stateHidden"
    

    From Android Documentation - adjustPan - The activity's main window is not resized to make room for the soft keyboard. Rather, the contents of the window are automatically panned so that the current focus is never obscured by the keyboard and users can always see what they are typing. This is generally less desirable than resizing, because the user may need to close the soft keyboard to get at and interact with obscured parts of the window.

    Edit 2 - Code for calculating the height of the Keyboard

    myLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    
                    @Override
                    public void onGlobalLayout() {
    
                        Rect r = new Rect();
                        parent.getWindowVisibleDisplayFrame(r);
    
                        int screenHeight = parent.getRootView().getHeight();
                        int heightDifference = screenHeight - (r.bottom - r.top);
                        Log.d("Keyboard Size", "Size: " + heightDifference);
    
                    }
                });
    

    Add that heightDifference by creating a View Programmatically and setting it's height.

    Edit 3 -

    Use this to hide the keyboard

    public static void hideKeyboardFrom(Context context, View view) {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
    

    Let me know if this works.