Share Coding

Tutorials, Problems, Stuffs …

Tag Archives: windowSoftInputMode

Android keyboard overlapping EditText box

This method only work after API level >= 21.
Add the following attribute to your activity. Every thing will be fine!

android:windowSoftInputMode="adjustResize"

Add the following into your root Layout at the xml file

android:fitsSystemWindows="true"

 

We should use AndroidBug5497Workaround.assistActivity to help us in
API level >= 19 && < 21, it can resize the windows automatically.
Add these code in your activity.

// Solve keyboard overlap textView problem after 4.4
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
        && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
    AndroidBug5497Workaround.assistActivity(this);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    // ADJUST_RESIZE can solve most of the device
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
}

Add the following into your root Layout at the xml file.

android:fitsSystemWindows="true"
import android.app.Activity;
import android.graphics.Rect;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;

public class AndroidBug5497Workaround {

    // For more information, see https://code.google.com/p/android/issues/detail?id=5497
    // To use this class, simply invoke assistActivity() on an Activity that already has its content view set.

    public static void assistActivity(Activity activity) {
        new AndroidBug5497Workaround(activity);
    }

    private View mChildOfContent;
    private int usableHeightPrevious;
    private FrameLayout.LayoutParams frameLayoutParams;

    private AndroidBug5497Workaround(Activity activity) {
        mChildOfContent = ((FrameLayout) activity.findViewById(android.R.id.content)).getChildAt(0);
        mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            public void onGlobalLayout() {
                possiblyResizeChildOfContent();
            }
        });
        frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();
    }

    private void possiblyResizeChildOfContent() {
        int usableHeightNow = computeUsableHeight();
        if (usableHeightNow != usableHeightPrevious) {
            int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();
            int heightDifference = usableHeightSansKeyboard - usableHeightNow;
            if (heightDifference > (usableHeightSansKeyboard / 4)) {
                // keyboard probably just became visible
                frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;
            } else {
                // keyboard probably just became hidden
                frameLayoutParams.height = usableHeightSansKeyboard;
            }
            mChildOfContent.requestLayout();
            usableHeightPrevious = usableHeightNow;
        }
    }

    private int computeUsableHeight() {
        Rect r = new Rect();
        mChildOfContent.getWindowVisibleDisplayFrame(r);
        return r.bottom;
    }

}

Besides, we need to add this style in your v19 and v21/style.xml
Add style in every dialog, don’t use default dialog.

<style name="CustomDialogTheme" parent="@style/Base.Theme.AppCompat.Light.Dialog">
    <item name="android:windowIsFloating">true</item>
    <item name="android:layout_margin">3dp</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
    <item name="android:fitsSystemWindows">true</item>

    <!-- <item name="android:colorBackgroundCacheHint">#00000000</item> -->
    <item name="android:background">#00000000</item>

</style>

Android keyboard overlap with the content

Write this on your activity, done.

android:windowSoftInputMode = "adjustResize"

If not working, your root element at xml must contain the following attribute.

android:fitSysytemWindow="true"