Is there a way in Android to get the size of the soft buttons bar and status bar togheter? or a way to go get the screen height without the height of both these bars? (they are not always the same, look at the nexus 7 for example)
4 Answers
Height of the Status Bar
The height of the status bar depends on the screen size, for example in a device with 240 X 320 screen size the status bar height is 20px, for a device with 320 X 480 screen size the status bar height is 25px, for a device with 480 x 800 the status bar height must be 38px
so I recommend to use this script to get the status bar height
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("*** Value :: ", "StatusBar Height= " + statusBarHeight + " , TitleBar Height = " + titleBarHeight);
to get the Height of the status bar on the onCreate() method of your Activity, use this method:
public int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
Soft Key Buttons Bar
This method is very useful in order to set the layout padding in Android KitKat (4.4). Using this, you can avoid the soft buttons bar overlapping over your layout.
The getRealMetrics method is only available with API 17 and +
@SuppressLint("NewApi")
private int getSoftButtonsBarHeight() {
// getRealMetrics is only available with API 17 and +
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int usableHeight = metrics.heightPixels;
getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
int realHeight = metrics.heightPixels;
if (realHeight > usableHeight)
return realHeight - usableHeight;
else
return 0;
}
return 0;
}
Reference:
- 1
- 1
- 9,598
- 13
- 48
- 65
-
2Wow thanks alot for this answer. I thought the second method wouldn't work in case you have both a status and softbutton bar at the same time, guess i was wrong, i've already tested this and it works. :D Even better, it works and API 16 too! – Joske369 Apr 01 '15 at 21:15
-
There's just one problem when the statusbar is the soft button bar, the first method returns 25 pixels and the softbuttons method return 48 pixels (sony xperia tablet s) – Joske369 Apr 01 '15 at 21:34
-
@Joske369 read the links in the reference, you can find others codes – Adrian Cid Almaguer Apr 01 '15 at 21:35
for me, in android note 10, I need to get navigation bar height by dimen:
fun getSoftNavigationBarSize(resources: Resources): Int {
var result = 0
val resourceId: Int = resources.getIdentifier("navigation_bar_height", "dimen", "android")
if (resourceId > 0) {
result = resources.getDimensionPixelSize(resourceId)
}
return result
}
- 602
- 8
- 10
I know this has been answered already, but I just found this solution that seems easier than the others. I only tried it for the bottom button bar. I'm pretty sure that it works the same way for the status bar though.
view.rootWindowInsets?.let { inset ->
Log.e("XXX", "button bar height: ${inset.systemWindowInsetBottom}")
}
Note: you need a view that is attached to the window at this time.
- 10,606
- 5
- 40
- 51
private void getStatusAndNavigationBarsHeight() {
float deviceDensity = getResources().getDisplayMetrics().density;
float statusHeight = 0;
float navigationHeight = 0;
Resources myResources = getApplicationContext().getResources();
int idStatusBarHeight = myResources.getIdentifier( "status_bar_height", "dimen", "android");
statusHeight = idStatusBarHeight > 0 ? getResources().getDimensionPixelSize(idStatusBarHeight)/deviceDensity : 0;
statusHeight = Math.round(statusHeight);
Log.d("StatusBar Height(dp):", String.valueOf(statusHeight));
int idNavigationBarHeight = myResources.getIdentifier("navigation_bar_height", "dimen", "android");
navigationHeight = idNavigationBarHeight > 0 ? getResources().getDimensionPixelSize(idNavigationBarHeight)/deviceDensity : 0;
navigationHeight = Math.round(navigationHeight);
Log.d("Navigation Bar Height(dp):", String.valueOf(navigationHeight));
}
- 31
- 2
-
-
statusHeight = idStatusBarHeight > 0 ? getResources().getDimensionPixelSize(idStatusBarHeight)/deviceDensity : 0; navigationHeight = idNavigationBarHeight > 0 ? getResources().getDimensionPixelSize(idNavigationBarHeight)/deviceDensity : 0; – Nag Kosanam May 17 '21 at 11:21
-
Here we are dividing by deviceDensity so the values returned looks more realistic. – Nag Kosanam May 17 '21 at 11:24