The layout calculation problem of Android soft keyboard in fullscreen

Source: Internet
Author: User

In non-full-screen mode, set the activity's Windowsoftinputmode property to: Adjustresize. At the same time, the onsizechanged (int w, int h, int oldw, int oldh) in the view can get the changed size, and then calculate the distance that the screen needs to move according to the result of the change before and after.

However, in full-screen mode, even if the activity's Windowsoftinputmode property is set to: Adjustresize. It does not push the activity screen upward when the keyboard is displayed, so the size of the root tree of your activity's view is unchanged. In this case, you will not know the size of the keyboard, the root view of the corresponding changes. The problem that the keyboard can't resize in full screen has existed since 2.1, and Google hasn't solved it yet.

If you want to learn about the general situation of Android Soft keyboard display with hidden issues, you can learn this blog on Android soft keyboard display with hidden problem research, it can help you to solve the problem of layout when the soft keyboard is displayed in a non full screen, you can't control it well.

Thanks to Ricardo for the wheel, he found a solution in StackOverflow. Someone has encapsulated the class, you just have to refer to OK, let's look at this helper class.

Help class Source:

Workaround to get adjustresize functionality for input Methos while the fullscreen mode is on//found by Ricardo//taken fr Om 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//T    o Use the This class, simply invoke Assistactivity () on the Activity that already have 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 (and Roid.        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 (). Get            Height ();            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 = Usableheightsans            Keyboard;         }   Mchildofcontent.requestlayout ();        usableheightprevious = Usableheightnow;        }} private int computeusableheight () {rect r = new Rect ();        Mchildofcontent.getwindowvisibledisplayframe (R);    return (R.bottom-r.top); }}

You can see the principle from the source code.

1. Use Viewtreeobserver to monitor the change of the view root tree. Once the keyboard is displayed or hidden, it causes the Addongloballayoutlistener () method to be called.

2. Calculate the height of the activtiy viewable area and compare it with the last visible area height, if equal, the interface appears to change.

3. If the interface changes, calculate the difference between the visible area height visibleheight and the activity root view height rootheight heightdifference.

4. If heightdifference > (ROOTHEIGHT/4), the keyboard is considered to be displayed, otherwise the keyboard is considered hidden.

5. Depending on the state of the keyboard changes, the contentview of the activity is highly attribute-changed.

At this point, you have finished showing or hiding the resize to Contentview on the keyboard.


How to use

Call Androidbug5497workaround.assistactivity (this) in the OnCreate () method of your activity; Note: 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.