adjustResize does not work with CoordinatorLayout

后端 未结 4 854
无人及你
无人及你 2021-02-06 06:49

I have the following layout in my android app but I have a problem with windowSoftInputMode=\"adjustResize\" in the activity: The LinearLayout named \"container\" c

4条回答
  •  不思量自难忘°
    2021-02-06 07:13

    This is from another answer. Worked perfectly for me.

    import android.app.Activity;
    import android.graphics.Rect;
    import android.os.Build;
    import android.view.View;
    import android.view.ViewTreeObserver;
    import android.view.inputmethod.InputMethodManager;
    
    public class KeyboardUtil {
        private View decorView;
        private View contentView;
    
        public KeyboardUtil(Activity act, View contentView) {
            this.decorView = act.getWindow().getDecorView();
            this.contentView = contentView;
    
            //only required on newer android versions. it was working on API level 19
            if (Build.VERSION.SDK_INT >= 19) {
                decorView.getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener);
            }
        }
    
        public void enable() {
            if (Build.VERSION.SDK_INT >= 19) {
                decorView.getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener);
            }
        }
    
        public void disable() {
            if (Build.VERSION.SDK_INT >= 19) {
                decorView.getViewTreeObserver().removeOnGlobalLayoutListener(onGlobalLayoutListener);
            }
        }
    
    
        //a small helper to allow showing the editText focus
        ViewTreeObserver.OnGlobalLayoutListener onGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect r = new Rect();
                //r will be populated with the coordinates of your view that area still visible.
                decorView.getWindowVisibleDisplayFrame(r);
    
                //get screen height and calculate the difference with the useable area from the r
                int height = decorView.getContext().getResources().getDisplayMetrics().heightPixels;
                int diff = height - r.bottom;
    
                //if it could be a keyboard add the padding to the view
                if (diff != 0) {
                    // if the use-able screen height differs from the total screen height we assume that it shows a keyboard now
                    //check if the padding is 0 (if yes set the padding for the keyboard)
                    if (contentView.getPaddingBottom() != diff) {
                        //set the padding of the contentView for the keyboard
                        contentView.setPadding(0, 0, 0, diff);
                    }
                } else {
                    //check if the padding is != 0 (if yes reset the padding)
                    if (contentView.getPaddingBottom() != 0) {
                        //reset the padding of the contentView
                        contentView.setPadding(0, 0, 0, 0);
                    }
                }
            }
        };
    
    
        /**
         * Helper to hide the keyboard
         *
         * @param act
         */
        public static void hideKeyboard(Activity act) {
            if (act != null && act.getCurrentFocus() != null) {
                InputMethodManager inputMethodManager = (InputMethodManager) act.getSystemService(Activity.INPUT_METHOD_SERVICE);
                inputMethodManager.hideSoftInputFromWindow(act.getCurrentFocus().getWindowToken(), 0);
            }
        }
    }
    

    You can then use it in your activity by doing the following:

    //initialize the KeyboardUtil (you can do this global)

    KeyboardUtil keyboardUtil = new KeyboardUtil(this, findViewById(android.R.id.content));
    
    //enable it
    keyboardUtil.enable();
    

    To disable this resizing, do this

    //disable it
    keyboardUtil.disable();
    

提交回复
热议问题