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!
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.
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
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).
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.
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;
}
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;
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With