Skip to content

Instantly share code, notes, and snippets.

@hamakn
Created April 6, 2015 02:41
Show Gist options
  • Save hamakn/8939eb68a920a6d7a498 to your computer and use it in GitHub Desktop.
Save hamakn/8939eb68a920a6d7a498 to your computer and use it in GitHub Desktop.
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
Copy link

Thanks for showing this!

@DuyTang1112
Copy link

thanks a lot!

@yahim91
Copy link

yahim91 commented Mar 29, 2017

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

@LeoDroidCoder
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
Copy link

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

@shikechen
Copy link

@yahim91 get navigation height code works fine for Google Pixel

@DaveChambers
Copy link

Nice!

@chathura2020
Copy link

Thanks

@FSPinho
Copy link

FSPinho commented Jun 2, 2018

Thanks =)

@btow
Copy link

btow commented Jun 27, 2018

Super! Thenks!

@ahmaducg
Copy link

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
Copy link

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

@Dooks123
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;

@gabriellemos
Copy link

An update based on droidcon NYC 2017

mView.setOnApplyWindowInsetsListener((view, insets) -> {
    // insets.getSystemWindowInsetTop();
    // insets.getSystemWindowInsetRight();
    // insets.getSystemWindowInsetBottom();
    // insets.getWindowDecorInsetLeft();
    return insets;
});

@ezitoir
Copy link

ezitoir commented Mar 9, 2022

@yahim91 get navigation height code works fine for Google Pixel

this convert to pixel
float statusBarHeight = 0;
int resourceId = getActivity(this.context).getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
statusBarHeight = getActivity(this.context).getResources().getDimensionPixelSize(resourceId) / getActivity(this.context).getResources().getDisplayMetrics().density ;
}

@avetharun
Copy link

JNI translation for getting status bar height: (tested with a NativeActivity app)

static int AndroidStatusBarShown(int* height = NULL) {
    JNIEnv* jni;
    g_App->activity->vm->AttachCurrentThread(&jni, NULL);
    bool state = false;
    jclass cls = jni->GetObjectClass(g_App->activity->clazz);
    jmethodID getResourcesMID = jni->GetMethodID(cls, "getResources", "()Landroid/content/res/Resources;");
    jobject resources = jni->CallObjectMethod(g_App->activity->clazz, getResourcesMID);
    jclass resources_cls = jni->GetObjectClass(resources);
    jmethodID getIDMID = jni->GetMethodID(resources_cls, "getIdentifier", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)I");
    jmethodID getDimensionPixelSizeMID = jni->GetMethodID(resources_cls, "getDimensionPixelSize", "(I)I");
    jstring id0 = jni->NewStringUTF("status_bar_height");
    jstring id1 = jni->NewStringUTF("dimen");
    jstring id2 = jni->NewStringUTF("android");
    /*get the resource id for the status bar */
    jint resid = jni->CallIntMethod(resources, getIDMID, id0, id1, id2);
    int pxheight = 0;
    if (resid > 0) {
        pxheight = jni->CallIntMethod(resources, getDimensionPixelSizeMID, resid);
        /*get status bar height*/
    }
    if (height != NULL) {
        *height = pxheight;
    }
    jni->DeleteLocalRef(id0);
    jni->DeleteLocalRef(id1);
    jni->DeleteLocalRef(id2);
    g_App->activity->vm->DetachCurrentThread();
    return pxheight > 0;
}
static int AndroidStatusBarHeight() {
    int out;
    AndroidStatusBarShown(&out);
    return out;
}

@arab-ware
Copy link

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

You can read the android open source project , it contains the java & xml files of the whole system

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment