Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Android: Get height of status, action, navigation bar (pixels)
// status bar height
int statusBarHeight = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
statusBarHeight = getResources().getDimensionPixelSize(resourceId);
}
// action bar height
int actionBarHeight = 0;
final TypedArray styledAttributes = getActivity().getTheme().obtainStyledAttributes(
new int[] { android.R.attr.actionBarSize }
);
actionBarHeight = (int) styledAttributes.getDimension(0, 0);
styledAttributes.recycle();
// navigation bar height
int navigationBarHeight = 0;
int resourceId = getResources().getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
navigationBarHeight = resources.getDimensionPixelSize(resourceId);
}
@AbrahamMont

This comment has been minimized.

Copy link

AbrahamMont commented Jun 20, 2016

Thanks for showing this!

@DuyTang1112

This comment has been minimized.

Copy link

DuyTang1112 commented Mar 17, 2017

thanks a lot!

@yahim91

This comment has been minimized.

Copy link

yahim91 commented Mar 29, 2017

Does this work for Google Pixel (or Google Pixel emulator)?

@LeoDroidCoder

This comment has been minimized.

Copy link

LeoDroidCoder commented Aug 25, 2017

Not a reliable of getting navigation bar height!
On samsung devises it returns some height whilst there is no any navigation bar!

@shikechen

This comment has been minimized.

Copy link

shikechen commented Aug 31, 2017

In Line 20, "resources" should be as "getResources()"

@shikechen

This comment has been minimized.

Copy link

shikechen commented Aug 31, 2017

@yahim91 get navigation height code works fine for Google Pixel

@DaveChambers

This comment has been minimized.

Copy link

DaveChambers commented Feb 10, 2018

Nice!

@chathura2020

This comment has been minimized.

Copy link

chathura2020 commented Mar 24, 2018

Thanks

@FSPinho

This comment has been minimized.

Copy link

FSPinho commented Jun 2, 2018

Thanks =)

@btow

This comment has been minimized.

Copy link

btow commented Jun 27, 2018

Super! Thenks!

@ahmaducg

This comment has been minimized.

Copy link

ahmaducg commented Feb 27, 2019

For some reason this isn't working on S8 with Pie update. Any reason why?
It returns that the device has a navigation bar even though the system has hid it and replaced it with the full gesture navigation.

@groomefynit

This comment has been minimized.

Copy link

groomefynit commented Apr 4, 2019

How you found these identifiers? Plz if you can share.

@Dooks123

This comment has been minimized.

Copy link

Dooks123 commented May 20, 2019

Apparently the correct way to do it is to SetOnApplyWindowInsetsListener and what I have seen in the WhatsApp code, they do it this way also.
https://stackoverflow.com/a/47125610/1876355

Another option is to do this:

Rect rectangle = new Rect();
Window window = getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
int statusBarHeight = rectangle.top;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.