Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to REALLY get the navigation bar height in Android

I own a HTC One A9 which has the ability to hide the navigation bar. In my app, I need to get the height of the navigation bar and set a padding corresponding to it. Here's my problem now: When I hide the navigation bar, the padding is still being set (even Snapchat has this problem). My question is: Is there alternative code to this one that makes it work?

public static int getNavBarHeight(Context context) {
    int result = 0;
    int resourceId = context.getResources().getIdentifier("navigation_bar_height", "dimen", "android");
    if (resourceId > 0) {
        result = context.getResources().getDimensionPixelSize(resourceId);
    }
    return result;
}

Thanks for your help!

like image 484
Made by FA Avatar asked Apr 09 '16 07:04

Made by FA


People also ask

How to get the height and width of the navigation bar programmatically?

This example demonstrates how do I get the height and width of the android navigation bar programmatically. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml.

How to get the action bar height in Android?

This example demonstrates how do I get the action bar height in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. Step 3 − Add the following code to src/MainActivity.java

What value should I Set my navigation bar to on Android?

You can use the values between 210 to -210. Exceeding those values won’t hurt your device, but won’t have any impact on the navbar position either. A positive value would push the navigation bar up, while negative value would push it down (partially/fully hiding it even, of course).

What is the best value to hide the navigation bar?

I am using -126 value on my S9+ to completely hide the navigation bar and then use an app like Multi-action Home Button app to use the phone. Our reader, Adam Bosworth, is finding the value of -25 good to reduce the size of the navbar.


2 Answers

Use following code to get Navigation bar. However, you need to consider Multi window mode as well as whether Navigation bar is at the bottom or on left side or right side of the window.

getNavigationBarHeight(){
    Resources resources = context.getResources();
    int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
    if (resourceId > 0) {
        return resources.getDimensionPixelSize(resourceId);
    }
    return 0;
}
like image 128
mayank1513 Avatar answered Oct 05 '22 04:10

mayank1513


Here is the simplest answer. You just need to pass the rootView of the activity. The heights will be 0 if the the bars are not shown.

View rootView;
ViewCompat.setOnApplyWindowInsetsListener(rootView, (v, insets) -> {
    final int statusBarHeight = insets.getInsets(WindowInsetsCompat.Type.statusBars()).top; // in px
    final int navigationBarHeight = insets.getInsets(WindowInsetsCompat.Type.navigationBars()).bottom; // in px

    // do something with the heights

    return WindowInsetsCompat.CONSUMED;
});
like image 39
Faisal Khan Avatar answered Oct 05 '22 05:10

Faisal Khan