Size of android notification bar and title bar?
Maybe this is a helpful approach: Referring to the Icon Design Guidelines there are only three different heights for the status (notification) bar depending on the screen density:
- 24px for LDPI
- 32px for MDPI
- 48px for HDPI
So if you retrieve the screen density of the device using densityDpi
of DisplayMetrics you know which value to subtract
so it could look something like that:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int myHeight = 0;
switch (metrics.densityDpi) {
case DisplayMetrics.DENSITY_HIGH:
Log.i("display", "high");
myHeight = display.getHeight() - 48;
break;
case DisplayMetrics.DENSITY_MEDIUM:
Log.i("display", "medium/default");
myHeight = display.getHeight() - 32;
break;
case DisplayMetrics.DENSITY_LOW:
Log.i("display", "low");
myHeight = display.getHeight() - 24;
break;
default:
Log.i("display", "Unknown density");
Martin's answer specified the wrong height (at least as of SDKv11). As per Icon Design Guidelines, the status bar icons have a height of 25dp, not 32dp. 25dp translates into these density-specific heights:
- 19px for LDPI
- 25px for MDPI
- 38px for HDPI
- 50px for XHDPI
An easy way to observe these sizes is to use the hierarchyviewer against an emulator or device with a normal density. Simply look at the value of getHeight()
for the title bar's FrameLayout. The status bar is a bit trickier because it's part of the DecorView and not a view in itself, so get its height indirectly by looking at mTop
for the title bar's FrameLayout, which is positioned immediately below the status bar. Per my observations the status bar and title bar each match the 25dp height of the status bar icons (I have not seen a declaration of this fact in the android ref as of SDKv11).
*As for as I know, this works perfectly.
public int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}*
I use the following code for getting heights:
For Status (Notification) bar:
View decorView = getWindow().getDecorView();
Rect rect = new Rect();
decorView.getWindowVisibleDisplayFrame(rect);
int statusBarHeight = rect.top;
For Title bar:
View contentView = getWindow().findViewById(Window.ID_ANDROID_CONTENT);
int[] location = new int[2];
contentView.getLocationInWindow(location);
int titleBarHeight = location[1] - statusBarHeight;