Show entire bottom sheet with EditText above Keyboard

后端 未结 8 1367
北恋
北恋 2020-11-30 21:27

I\'m implementing a UI where a bottom sheet will appear above the keyboard with an EditText for the user to enter a value. The problem is the View is being partially overlap

8条回答
  •  忘掉有多难
    2020-11-30 21:50

    My answer might be useful for someone who is still looking for solution. If keyboard is covering edittext in BottomSheetDialogFragment then in setupDialog() method create instance of a class KeyboardUtil and pass your rootview.

        @Override
        public void setupDialog(final Dialog dialog, int style) {
            super.setupDialog(dialog, style);
            View view = View.inflate(getActivity(), R.layout.reopen_dialog_layout, null);
            new KeyboardUtil(getActivity(), view);
    }
    

    Create a new class

        public class KeyboardUtil {
            private View decorView;
            private View contentView;
            //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);
                        }
                    }
                }
            };
    
            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);
                }
            }
    
            /**
             * 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);
                }
            }
    
            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);
                }
            }
        }
    

提交回复
热议问题