22

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)

Adrian Cid Almaguer
  • 7,815
  • 13
  • 41
  • 63
Joske369
  • 505
  • 1
  • 6
  • 18

4 Answers4

2

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
}
Ho Luong
  • 726
  • 8
  • 12
  • 2
    This gives a warning: "Using internal inset dimension resource navigation_bar_height is not supported". Has this been deprecated, or is there another way to do this now? – SMBiggs Jan 27 '23 at 16:34
0

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.

SimonSays
  • 10,867
  • 7
  • 44
  • 59
0
 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));
    }
  • StatusBar Height(dp) : 33 Navigation Bar Height(dp): 48 – Nag Kosanam May 17 '21 at 11:18
  • 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
0

I would like to add one more way to @Adrian Cid Almaguer's answer as I wanted to get status-bar height in background service.

val windowService = context.getSystemService(Context.WINDOW_SERVICE)
(windowService as WindowManager).apply {
   val statusBar = currentWindowMetrics.windowInsets.getInsets(WindowInsets.Type.statusBars())
   val statusBarHeight = statusBar.top
}

And using the same method you can get Navigation Bar position, height, or width as well. (width if Navigation Bar is on the side of the screen)

var navBarHeight = 0
val navBar = currentWindowMetrics.windowInsets.getInsets(WindowInsets.Type.navigationBars())
when {
  (navBar.bottom > 0) -> navBarHeight = navBar.buttom
  (navBar.right > 0) -> navBarHeight = navBar.right 
  (navBar.left > 0) -> navBarHeight = navBar.left 
 }
Divyang M
  • 101
  • 1
  • 5
  • Hello @Divyang M, you are not mentioning that currentWindowMetrics requires SDK 30, so this solution is not applicable to older Android versions. – Diego Perez Aug 21 '22 at 12:32