Reliability of getting status bar height to solve KitKat transparent navigation problem

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

The code I use for this:

 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 ran into is that Android now thinks that the Activity is full-screen and places the layout behind the navigation bar (which is true), unfortunately, it also places the layout behind the status bar (problem).

A hack fix for this would be to apply an addition to the top of the layout of the parent view, however I need to determine the height of the status bar to do this.

Can anyone suggest how I get the height of the status bar, this is not as trivial as I thought it would be or, alternatively, offer the right solution.

thank

+23
android android-layout view android-4.4-kitkat
Dec 14 '13 at 14:12
source share
5 answers
 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 the contextWrapper class. http://mrtn.me/blog/2012/03/17/get-the-height-of-the-status-bar-in-android/

+54
Dec 14 '13 at 14:17
source share

Since api 21 has an official method for extracting attachments for the status bar and the height of the navigation bar when it 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; } }); 
+18
Jan 05 '16 at 15:01
source share

The height of the lower navigation bar is 48 dp (both in portrait and landscape modes) and is 42dp when the panel is placed vertically.

Hope this helps.

+3
Feb 05 '15 at
source share

The accepted answer always returns the height of the status bar (and in a somewhat hacky way). But some actions can indeed be full-screen, and this method does not distinguish between them.

This method is great for me to find the height of the status bar relative to the current activity (put it in your Activity class and use it after the layout is complete):

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

Note that you can also simply use displayRect directly if you have other “window decorations” at the bottom or potentially even on the sides of the screen.

+2
Feb 16 '16 at 15:07
source share

we recommend using 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 of 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; } 
0
Aug 04 '16 at 10:38 on
source share



All Articles