Reliably get height of status bar to solve KitKat translucent navigation issue

30,167

Solution 1

public int getStatusBarHeight() {
      int result = 0;
      int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
      if (resourceId > 0) {
          result = getResources().getDimensionPixelSize(resourceId);
      }
      return result;
}

Use the above code in the onCreate method. Put it in a contextWrapper class. http://mrtn.me/blog/2012/03/17/get-the-height-of-the-status-bar-in-android/

Solution 2

Since api 21 there is official method for retrieving insets for status bar and navigation bar height when is translucent

ViewCompat.setOnApplyWindowInsetsListener(view, new OnApplyWindowInsetsListener() {
        @Override
        public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
            final int statusBar = insets.getSystemWindowInsetTop();
            final int navigationBar = insets.getSystemWindowInsetBottom();
            return insets;
        }
    });

Solution 3

The accepted answer always returns the status bar height (and in a somewhat hacky way). But some activities may actually be fullscreen, and this method doesn't differentiate between them.

This method works perfectly for me to find the status bar height relative to the current activity (place it in your Activity class, and use it once layout has finished):

public int getStatusBarHeight() {
    Rect displayRect = new Rect();
    getWindow().getDecorView().getWindowVisibleDisplayFrame(displayRect);
    return displayRect.top;
}

Note you could also just use displayRect directly in case you have other "window decorations" at the bottom or potentially even the sides of the screen.

Share:
30,167

Related videos on Youtube

Milo
Author by

Milo

Updated on July 18, 2022

Comments

  • Milo
    Milo almost 2 years

    I am experimenting with the new Android 4.4 translucent navigation bars and would like to set the navigation bar as translucent using the FLAG_TRANSLUCENT_NAVIGATION flag. I only wish the navigation bar (back, home button etc) to be translucent - I want the status bar at the top of the screen to appear normally I.e. NOT translucent.

    The code I am using to achieve this is:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        Window w = getWindow();
        w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
    }
    

    The problem I have is Android now thinks the Activity is fullscreen and places the layout behind the navigation bar (which is correct), unfortunately it also places the layout behind the status bar (a problem).

    A hacky fix for this would be to apply a padding to the top of the layout parent View, however I need to determine the height of the status bar to do this.

    Could anyone suggest how I get the status bar height, it's not as trivial as I thought it would be, or alternatively suggest a proper solution.

    Thanks

    • noni
      noni over 10 years
      Hi Milo, I'm facing the same problem here. I've tried this workaround but my Layout keeps behind my ActionBAr. I've added a top padding to my root layout but I think the Status Bar height is not enough, do you have the same problem? I need to set a top padding for both heigths?
    • Milo
      Milo over 10 years
      Noni, I get the root view of the Activity as follows: getWindow().getDecorView().findViewById(android.R.id.content‌​); I then apply padding I.e. setPadding(0, statusBarHeight, 0, 0); Hope that helps.
  • Milo
    Milo over 10 years
    This does seem to work, do you know if this will work on every Android version 4.1 and above? I do remember reading this solution may not work on Transformer tablets, it makes me wonder if this'll fail on other devices too?
  • user2511882
    user2511882 over 10 years
    its only for the transformer tablet as far as i know. Works fine on other devices that i have tested. And dont forget to accept and upvote. cheers
  • Milo
    Milo over 10 years
    It's a pity there isn't a reliable method to get the height of the status bar on every device. That said, it'll have to do for now.
  • user2511882
    user2511882 over 10 years
    Yea that only because its not available publicly to the developers as part of the API. Its just a hack for getting around it. Hopefully there will be a official support for the next SDK release.
  • frostymarvelous
    frostymarvelous about 9 years
    just to add, the height is basically 25dip. I checked the source. However, the guidelines give the Status bar 24dp google.com/design/spec/layout/…
  • Petrus
    Petrus over 7 years
    Super useful! The only thing that works as expected in multi window mode with Android 7.0.
  • Tom
    Tom over 7 years
    Height is 24dp in Marshmellow+
  • jk7
    jk7 almost 7 years
    This works for 10.1 WXGA table emulator which in API 16 does not display a status bar because it puts status info in bottom nav bar. Still looking for a way to determine status bar height and/or display status before layout.
  • urSus
    urSus over 6 years
    that kind of view is "view"?
  • Charles Madere
    Charles Madere almost 6 years
    I'd love to see more documentation / information on this. getSystemWindowInsetTop() seems like such a weird name for "status bar height".
  • Jacek Marchwicki
    Jacek Marchwicki almost 6 years
    This is a little tricky because Google assumed the status bar is not always above your content (Remember Android Honeycomb UI?). The same with the navigation bar - it can appear on the right when you rotate the screen. You shouldn't care where they are, the system will tell you where you should apply your paddings. Look here for more information, read this article: medium.com/google-developers/…
  • Yusril Maulidan Raji
    Yusril Maulidan Raji over 5 years
    Somehow, the onApplyWindowInsets() is not triggered in my case. I set this inside onCreateView() in a fragment. Do you have any clue?
  • neoexpert
    neoexpert over 4 years
    On Galaxy s10 is the statusbar wider, because of the camera hole