Reliably Get Height Of Status Bar To Solve Kitkat Translucent Navigation Issue
Solution 1:
publicintgetStatusBarHeight() {
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, newOnApplyWindowInsetsListener() {
@Overridepublic WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
finalintstatusBar= insets.getSystemWindowInsetTop();
finalintnavigationBar= 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):
publicintgetStatusBarHeight() {
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.
Solution 4:
recommend to use this script to get the status bar height
Rectrectangle=newRect();
Windowwindow= getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
intstatusBarHeight= rectangle.top;
intcontentViewTop=
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:
publicintgetStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
Post a Comment for "Reliably Get Height Of Status Bar To Solve Kitkat Translucent Navigation Issue"