Phonegap - Android How to adjust layout in Full Screen Mode when softkeyboard is visible

*爱你&永不变心* 提交于 2019-11-30 07:32:08
Jorge Marmolejo

After spending a day trying almost every possible solution in this website, nothing worked for me. At the end I was able to find a work around based on the following two proposed solutions:

https://stackoverflow.com/a/19494006/1435991

this link shows a workaround to fix the problem for an android app; however I don't have any experience working in android, so the question is: how to include this peace of code in a Phonepap project?.

https://stackoverflow.com/a/18610405

This link suggests specifically a solution for phonegap, which did not work for me, but more important gave an idea of how I could add custom android code on a phonegap project.

SOLUTION

1- Create the following class(as indicate in first link) in your phonegap project:



    package com.test.android;

    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) {
            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);
        }

}


This class can be placed in this location in your project: (I was not able to load an image in this forum, I need at lease 10 reputation). Find the image sample in this url:

2- Any time you create a phonegap project you get a class called your_project_name.java. In my case it is test1.java. Edit the class and add the following sentence in method onCreate:


    AndroidBug5497Workaround.assistActivity(this);

 

Your code should look like this:


    public class test1 extends DroidGap
    {
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            // Set by  in config.xml
            super.loadUrl(Config.getStartUrl());
            //super.loadUrl("file:///android_asset/www/index.html")
            AndroidBug5497Workaround.assistActivity(this);
        }
    }
     

3- This fixed the problem in my app.

Shatran

I Implemented Jorge's answer and it worked great for me!. but the screen resize itself in a bulky way, and i wanted it to resize more smoothly. So i looked up on how to animate this view and came across this.

combining the two together looks like this:

import android.animation.ValueAnimator;
import android.app.Activity;
import android.graphics.Rect;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.animation.DecelerateInterpolator;
import android.widget.FrameLayout;

public class AdjustInputHeight {

    // 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 AdjustInputHeight(activity);
    }

    private View mChildOfContent;
    private int usableHeightPrevious;
    private ValueAnimator animateCollapseView = null;
    private ValueAnimator animateExpandView = null;
    private boolean keyboardIsUp = false;
    DecelerateInterpolator sDecelerator = new DecelerateInterpolator();
    private FrameLayout.LayoutParams frameLayoutParams;

    private AdjustInputHeight(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;

            //check if the view got smaller (because keyboard is shown) and is not already up.
            if (heightDifference > (usableHeightSansKeyboard/4) && (!this.keyboardIsUp)) {

                // we need to create the collapse animator the only the first time we rise the keyboard
                if (this.animateCollapseView == null) {
                    this.animateCollapseView = ValueAnimator.ofInt(usableHeightSansKeyboard, (usableHeightSansKeyboard-heightDifference));
                    this.animateCollapseView.setDuration(500);
                    this.animateCollapseView.setInterpolator(sDecelerator);
                    this.animateCollapseView.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                        public void onAnimationUpdate(ValueAnimator animation) {
                            Integer value = (Integer) animation.getAnimatedValue();
                            frameLayoutParams.height = value.intValue();
                            mChildOfContent.requestLayout();
                        }
                    });
                }

                this.animateCollapseView.start();
                // keyboard probably just became visible        
                this.keyboardIsUp = true;

            //lower the keyboard only if it is up.  
            } else if (this.keyboardIsUp) {

                // we need to create the expand animator the only the first time we lower the keyboard
                if (this.animateExpandView == null) {   
                    this.animateExpandView = ValueAnimator.ofInt((usableHeightSansKeyboard-heightDifference), usableHeightSansKeyboard);
                    this.animateExpandView.setDuration(200);
                    this.animateExpandView.setInterpolator(sDecelerator);
                    this.animateExpandView.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                        public void onAnimationUpdate(ValueAnimator animation) {
                            Integer value = (Integer) animation.getAnimatedValue();
                            frameLayoutParams.height = value.intValue();
                            mChildOfContent.requestLayout();
                        }
                    });
                }

                this.animateExpandView.start();
                // keyboard probably just became hidden
                this.keyboardIsUp = false;
            }

            usableHeightPrevious = usableHeightNow;
        }
    }

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

}

of course you can change the animation duration and interpolator for your own needs.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!