Computing of the layout of the android soft keyboard in full screen mode

Source: Internet
Author: User

In non-full screen mode, set the activity's windowSoftInputMode attribute to adjustResize. At the same time, you can get the changed size in onSizeChanged (int w, int h, int oldw, int oldh) of the View, and then calculate the distance to move the screen based on the results of the changes.

However, in full screen mode, even if the activity's windowSoftInputMode attribute is set to adjustResize. When displayed on the keyboard, the Screen of the Activity is not pushed up, so the size of the root tree of the view of your Activity remains unchanged. In this case, you will not be able to know the keyboard size and proceed with the corresponding passage of the Root view. The problem of keyboard Resize failure in full screen mode exists from 2.1 Until now google has not solved it.

If you want to know about the display and hiding problems of the android soft keyboard in general circumstances, you can learn about the display and hiding problems of the android soft keyboard in this blog. It can help you solve the problem in non-full screen mode, when the keyboard is displayed, you cannot control the layout well.

Thanks to Ricardo for providing the wheel, he found a solution in stackoverflow. Someone has encapsulated this class, and you only need to reference it. Let's take a look at this help class.

Help class source code:

//Workaround to get adjustResize functionality for input methos when the fullscreen mode is on//found by Ricardo//taken from http://stackoverflow.com/a/19494006import 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) {        FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);        mChildOfContent = 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 - r.top);    }}

You can see the principle in the source code.

1. viewTreeobserver is used to monitor the changes in the View root tree. Once the keyboard is displayed or hidden, the addOnGlobalLayoutListener () method is called.

2. Calculate the height of the Activtiy visible area and compare it with that of the last visible area. If the height is equal, the interface changes.

3. If the page changes, calculate the height of the visible area visibleHeight and the height of the Activity Root view.

4. If heightDifference> (rootHeight/4), the keyboard is displayed; otherwise, the keyboard is hidden.

5. Change the height attribute of the Activity contentView Based on the keyboard status.

Now, you have finished displaying or hiding the contentView on the keyboard.


Usage

You can call AndroidBug5497Workaround. Define Activity (this); In the oncreate () method of your Activity. Note: It is called after setContentView (R. layout. xxx.


Example source code


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.