Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Height of statusbar?

Tags:

android

Is there a way to get the height of the statusbar + titlebar? Checking the dev forum shows the same question but no solution (that I could find).

I know we can get it after the initial layout pass, but I'm looking to get it in onCreate() of my activity.

Thanks

like image 213
user291701 Avatar asked Jul 28 '10 16:07

user291701


People also ask

What is status bar height?

Android status bar height: 24dp.

How do I get the StatusBar height in react native?

currentHeight : import {StatusBar} from 'react-native'; console. log('statusBarHeight: ', StatusBar. currentHeight);


2 Answers

Rect rectgle= new Rect(); Window window= getWindow(); window.getDecorView().getWindowVisibleDisplayFrame(rectgle); int StatusBarHeight= rectgle.top; int contentViewTop=      window.findViewById(Window.ID_ANDROID_CONTENT).getTop(); int TitleBarHeight= contentViewTop - StatusBarHeight;     Log.i("*** Jorgesys :: ", "StatusBar Height= " + StatusBarHeight + " , TitleBar Height = " + TitleBarHeight);  

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; }  
like image 93
Jorgesys Avatar answered Sep 29 '22 18:09

Jorgesys


For those, like me, who want to use it in your XML layouts:

<...   android:layout_marginTop="@*android:dimen/status_bar_height"   ... /> 

Don't be confused by that Android Studio (2.2 Preview 3 -- at the moment of writing) doesn't support this notion. The runtime does. I took it from the Google's source code.

like image 35
Aleksey Gureiev Avatar answered Sep 29 '22 16:09

Aleksey Gureiev