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

前端 未结 25 2308
后悔当初
后悔当初 2020-11-22 03:56

I have researched a lot to adjust the layout when softkeyboard is active and I have successfully implemented it but the problem comes when I use android:theme=\"@andro

25条回答
  •  闹比i
    闹比i (楼主)
    2020-11-22 04:34

    based on https://stackoverflow.com/a/19494006/1815624 and desire to make it happen...

    updated idea


    combining answers from

    • https://stackoverflow.com/a/19494006/1815624
    • https://stackoverflow.com/a/10952394/1815624

    Relevant code:

            if (heightDifference > (usableHeightSansKeyboard / 4)) {
    
                // keyboard probably just became visible
                frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;
                activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
                activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
            } else {
    
                // keyboard probably just became hidden
                if(usableHeightPrevious != 0) {
                    frameLayoutParams.height = usableHeightSansKeyboard;
                    activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
                    activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    
                }
    

    Full Source at https://github.com/CrandellWS/AndroidBug5497Workaround/blob/master/AndroidBug5497Workaround.java

    old idea

    Create a static value of the containers height before opening the keyboard Set the container height based on usableHeightSansKeyboard - heightDifference when the keyboard opens and set it back to the saved value when it closes

    if (heightDifference > (usableHeightSansKeyboard / 4)) {
                    // keyboard probably just became visible
                    frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;
                    int mStatusHeight = getStatusBarHeight();
                    frameLayoutParams.topMargin = mStatusHeight;
                    ((MainActivity)activity).setMyMainHeight(usableHeightSansKeyboard - heightDifference);
    
                    if(BuildConfig.DEBUG){
                        Log.v("aBug5497", "keyboard probably just became visible");
                    }
                } else {
                    // keyboard probably just became hidden
                    if(usableHeightPrevious != 0) {
                        frameLayoutParams.height = usableHeightSansKeyboard;
                        ((MainActivity)activity).setMyMainHeight();    
                    }
                    frameLayoutParams.topMargin = 0;
    
                    if(BuildConfig.DEBUG){
                        Log.v("aBug5497", "keyboard probably just became hidden");
                    }
                }
    

    Methods in MainActivity

    public void setMyMainHeight(final int myMainHeight) {
    
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ConstraintLayout.LayoutParams rLparams =  (ConstraintLayout.LayoutParams) myContainer.getLayoutParams();
                rLparams.height = myMainHeight;
    
                myContainer.setLayoutParams(rLparams);
            }
    
        });
    
    }
    
    int mainHeight = 0;
    public void setMyMainHeight() {
    
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ConstraintLayout.LayoutParams rLparams =  (ConstraintLayout.LayoutParams) myContainer.getLayoutParams();
                rLparams.height = mainHeight;
    
                myContainer.setLayoutParams(rLparams);
            }
    
        });
    
    }
    

    Example Container XML

    
            
    

    similarly margins can be added if needed...

    Another consideration is use padding an example of this can be found at:

    https://github.com/mikepenz/MaterialDrawer/issues/95#issuecomment-80519589

提交回复
热议问题