You can get the height of the status bar with the code:
Rect rectangle = new Rect(); Window window = activity.getWindow(); window.getDecorView().getWindowVisibleDisplayFrame(rectangle); int statusBarTop = rectangle.top; int contentViewTop = window.findViewById(Window.ID_ANDROID_CONTENT).getTop(); int statusbarHeight = Math.abs(statusBarTop - contentViewTop);
As for the action-bar, it has a constant abs__action_bar_default_height , you can dig to its side.
And now, when you know the height of the status bar, you can do this:
mMainLayout.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { 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 statusbarHeight = Math.abs(statusBarHeight - contentViewTop); int[] location = {0,0}; mMainLayout.getLocationOnScreen(location); int actionbarheight = Math.abs(location[0] -statusBarHeight); Toast.makeText(MainActivity.this, actionbarheight + "", 1).show(); } });
A source.