Search code examples
androidandroid-layoutandroid-actionbarandroid-titlebarandroid-statusbar

Why I get this difference of height values?


My screen is 1080x1920. Height of green line is 1794, red line - 63 (status bar) + 84 (TitleBar) = 147, violet line - 1584. Why I dont get height of green line by adding red one to the violet one?

enter image description here

Violet line represents height of relative layout and I find it with this code in the onWindowFocusChanged():

RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.myRelativeLayout);
Log.i(TAG, "Relative layout height: "+String.valueOf(relativeLayout.getHeight()));

Red line represents height of title bar and status bar and I find it with this code in the onWindowFocusChanged():

Rect rectangle = new Rect();
Window window = getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
int statusBarHeight = rectangle.top;
int contentViewTop = window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
int titleBarHeight= contentViewTop - statusBarHeight;

Log.i(TAG, "StatusBar Height= " + statusBarHeight + " , TitleBar Height = " + titleBarHeight);

Green line suppose to represent height of whole program? and I find it with this code in the onWindowFocusChanged():

Display mdisp = getWindowManager().getDefaultDisplay();
Log.i(TAG, "mdisp: "+String.valueOf(mdisp));
Point mdispSize = new Point();
mdisp.getSize(mdispSize);
int maxX = mdispSize.x;
int maxY = mdispSize.y;
Log.i(TAG, "Max X, Y: "+String.valueOf(maxX)+", "+String.valueOf(maxY));

XML layout file contents:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="lt.wilkas.stackoverflowquestion.MainActivity"
    android:id="@+id/myRelativeLayout">

</RelativeLayout>

Solution

  • Your titlebar height is not correct. As you can see, your titlebar height is visually appearing more than twice the height of your statusbar. So please use different method to get your titlebar height.

    You can get this like:

    int whiteParentHeight = 0;
    int titlebarHeight = 0;
    
    public int getTitleBarHeight() {
    
    whiteParent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
    
                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                    //noinspection deprecation
                    whiteParent.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                } else {
                    whiteParent.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                }
    
                whiteParentHeight = whiteParent.getHeight();
                titlebarHeight = whiteParentHeight - getStatusBarHeight();
    
            }
        });
    
    }
    
    
    public int getStatusBarHeight() {
    
        int result = 0;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
    
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
    
        return result;
    
    }
    

    or to find default titlebar height, you can use:

    final TypedArray styledAttributes = getContext().getTheme().obtainStyledAttributes(
                    new int[] { android.R.attr.actionBarSize });
    mActionBarSize = (int) styledAttributes.getDimension(0, 0);
    styledAttributes.recycle();
    

    Hope this will help you.