samples-keyboardheight icon indicating copy to clipboard operation
samples-keyboardheight copied to clipboard

It can't work well for the Android P which contain the cutout mode.

Open polar91760 opened this issue 7 years ago • 2 comments

Our suggestion is :
` int orientation = getScreenOrientation(); int topCutoutHeight = 0; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { topCutoutHeight = getTopCutoutHeight(); } int keyboardHeight = screenSize.y + topCutoutHeight - rect.bottom;

    @TargetApi(android.os.Build.VERSION_CODES.P)
    private int getTopCutoutHeight(){
        View decorView = activity.getWindow().getDecorView();
        if(decorView == null){
            return 0;
        }
        int cutOffHeight = 0;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            WindowInsets windowInsets = decorView.getRootWindowInsets();
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {
                DisplayCutout displayCutout = windowInsets.getDisplayCutout();
                if(displayCutout != null){
                    List<Rect> list = displayCutout.getBoundingRects();
                    for(Rect rect : list){
                        if(rect.top == 0){
                            cutOffHeight += rect.bottom - rect.top;
                        }
                    }
                }
            }

        }
        return cutOffHeight;
    }

`

polar91760 avatar Aug 21 '18 09:08 polar91760

What about on apps targeting lower APIs? DisplayCutout is not an option. How do we handle this?

rohangoqii avatar Oct 03 '18 09:10 rohangoqii

Our suggestion is : ` int orientation = getScreenOrientation(); int topCutoutHeight = 0; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { topCutoutHeight = getTopCutoutHeight(); } int keyboardHeight = screenSize.y + topCutoutHeight - rect.bottom;

    @TargetApi(android.os.Build.VERSION_CODES.P)
    private int getTopCutoutHeight(){
        View decorView = activity.getWindow().getDecorView();
        if(decorView == null){
            return 0;
        }
        int cutOffHeight = 0;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            WindowInsets windowInsets = decorView.getRootWindowInsets();
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {
                DisplayCutout displayCutout = windowInsets.getDisplayCutout();
                if(displayCutout != null){
                    List<Rect> list = displayCutout.getBoundingRects();
                    for(Rect rect : list){
                        if(rect.top == 0){
                            cutOffHeight += rect.bottom - rect.top;
                        }
                    }
                }
            }

        }
        return cutOffHeight;
    }

`

What about on apps targeting lower APIs? DisplayCutout is not an option. How do we handle this?

rohangoqii avatar Oct 09 '18 10:10 rohangoqii