Milo
Milo

Reputation: 1017

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

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

Upvotes: 27

Views: 30357

Answers (4)

Yogesh Rathi
Yogesh Rathi

Reputation: 6509

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("*** Elenasys :: ", "StatusBar Height= " + statusBarHeight + " , TitleBar Height = " + titleBarHeight); 

(old Method) 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;
}

Upvotes: 0

Mete
Mete

Reputation: 5635

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.

Upvotes: 3

Jacek Marchwicki
Jacek Marchwicki

Reputation: 1673

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;
        }
    });

Upvotes: 25

user2511882
user2511882

Reputation: 9152

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/

Upvotes: 61

Related Questions