How to detect bottom soft navigation bar available in android programmatically?
Asked Answered
S

8

44

I am trying to determine soft navigation bar through the android program. I didn't find straight way to determine. Is there anyway to find the navigation bar availability.

Soft Navigation bar image is here.

enter image description here

Skate answered 11/3, 2015 at 10:6 Comment(0)
S
78

Following method worked for me and tested in many devices.

public boolean hasNavBar (Resources resources)
    {
        int id = resources.getIdentifier("config_showNavigationBar", "bool", "android");
        return id > 0 && resources.getBoolean(id);
    }

Note: Verified this method in real device

Skate answered 21/9, 2015 at 14:55 Comment(8)
This code works but instead of 'android' use '"android"'Germanophobe
Returns false in both cases.Abide
Note that this always returns false on the Android emulators. See this commit message for an explanation.Apophysis
Despite not working on the emulators, this is very close to what the Android source code does, so it should be pretty accurate on real devices.Apophysis
checked in one plus 3 enabling both its always returns false.Mcshane
How can we get bar height in PX or DP?Laguna
Hello @MuhammadHannan, you could use the following method to get the any resource size in pixel. resources.getDimensionPixelSize(id);Skate
We can not use config_showNavigationBar or KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK) to detect navigation bar visible or not. Because this ways always returns true on some devices Huawei and Samsung such as A70. This is my solution: #20264768 . We can get real height of the navigation bar displaying on screen to detect it visible or not.Feral
H
27

As i know you can detect it by

boolean hasSoftKey = ViewConfiguration.get(context).hasPermanentMenuKey();

But it required APIs 14+


If above solution doesn't work for you then try below method

public boolean isNavigationBarAvailable(){

        boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
        boolean hasHomeKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_HOME);

        return (!(hasBackKey && hasHomeKey));
    }
Hairston answered 11/3, 2015 at 10:11 Comment(10)
Thanks for your response. I am looking to determine soft navigation bar presence.Skate
The device don't have permanentmenu key is not equalant to device have soft navigation bar. Some device don't have permanentmenu key and don't have soft navigation bar also. Please look this device gsmarena.com/asus_zenfone_5-pictures-5952.phpSkate
Thank you for such great information, because of this type of devices you can try my second solution (look updated answer)Hairston
Thanks man. Seems it will work for me. I tested on two available devices as expected.Skate
Enjoy programming, if this answer help you , then accepting answer or upvote will be very appreciated :-)Hairston
For me only the first solution works. The second failed on nexus 5Stringer
No idea why hasBackKey equals true on Sony Xperia Z3. Which make the second solution fail.Cathern
Thanks bro you saved my life !Malamut
This is not a correct answer! This solution only checks if device has physical navigation button, but there are also devices where both exists.Linville
I tested on Samsung A70 and some Huawei devices. This solution is incorrect, the method always returns true. My solution is base on the current height of navigation bar displaying on the screen to detect it's visible or not. #20264768Feral
T
17

Its a hack but it works fine. Try it.

public static boolean hasSoftKeys(WindowManager windowManager){
  Display d = windowManager.getDefaultDisplay();

  DisplayMetrics realDisplayMetrics = new DisplayMetrics();
  d.getRealMetrics(realDisplayMetrics);  

  int realHeight = realDisplayMetrics.heightPixels;
  int realWidth = realDisplayMetrics.widthPixels;

  DisplayMetrics displayMetrics = new DisplayMetrics();
  d.getMetrics(displayMetrics);

  int displayHeight = displayMetrics.heightPixels;
  int displayWidth = displayMetrics.widthPixels;

  return (realWidth - displayWidth) > 0 || (realHeight - displayHeight) > 0;
}
Tight answered 18/9, 2015 at 11:28 Comment(2)
Agree that its a hack , but a great one. KudosQuadrant
This will probably not work on the new Huawei P20 and other devices with a top notchTreasatreason
A
11

The accepted answer should work fine on most real devices, but it doesn't work in the emulators.

However, in Android 4.0 and above, there's an internal API that also works on the emulators: IWindowManager.hasNavigationBar(). You can access it using reflection:

/**
 * Returns {@code null} if this couldn't be determined.
 */
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@SuppressLint("PrivateApi")
public static Boolean hasNavigationBar() {
    try {
        Class<?> serviceManager = Class.forName("android.os.ServiceManager");
        IBinder serviceBinder = (IBinder)serviceManager.getMethod("getService", String.class).invoke(serviceManager, "window");
        Class<?> stub = Class.forName("android.view.IWindowManager$Stub");
        Object windowManagerService = stub.getMethod("asInterface", IBinder.class).invoke(stub, serviceBinder);
        Method hasNavigationBar = windowManagerService.getClass().getMethod("hasNavigationBar");
        return (boolean)hasNavigationBar.invoke(windowManagerService);
    } catch (ClassNotFoundException | ClassCastException | NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
        Log.w("YOUR_TAG_HERE", "Couldn't determine whether the device has a navigation bar", e);
        return null;
    }
}
Apophysis answered 25/5, 2017 at 0:15 Comment(6)
Of course, using internal api sort of stings, but hey, it works across all my devices (at least up to API25). Extra + for not being Context-dependent.Muckraker
it is working for me but I am getting a warning about I can get a null pointer exception. Is it safe to use?Spathose
@Dahnark it's an internal API, so there's no guarantee it will work on a given device. The method in my example returns null so you can detect when it didn't work. You need to decide what to do when that happens and add code for it.Apophysis
@Apophysis I know, but is it possible to avoid this warning? really I am using if-else to the three possibilities (has nav, hasn't nav, null), but I am still getting the warning.Spathose
@Dahnark Oh, I see. Don't know sorry.Apophysis
Is there any way to get Notification bar is visible or not in Android P?Teilo
M
4

Try this method,in this way you can detect if the navigation bar exist.

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public boolean hasNavBar(Context context) {
    Point realSize = new Point();
    Point screenSize = new Point();
    boolean hasNavBar = false;
    DisplayMetrics metrics = new DisplayMetrics();
    this.getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
    realSize.x = metrics.widthPixels;
    realSize.y = metrics.heightPixels;
    getWindowManager().getDefaultDisplay().getSize(screenSize);
    if (realSize.y != screenSize.y) {
        int difference = realSize.y - screenSize.y;
        int navBarHeight = 0;
        Resources resources = context.getResources();
        int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
        if (resourceId > 0) {
            navBarHeight = resources.getDimensionPixelSize(resourceId);
        }
        if (navBarHeight != 0) {
            if (difference == navBarHeight) {
                hasNavBar = true;
            }
        }

    }
    return hasNavBar;

}
Macroclimate answered 10/2, 2016 at 12:22 Comment(1)
Great work! But you should adjust your code a little bit as this will only work for portrait mode, but the device might also be in landscape orientation.Coverlet
S
2

Right answer and other are not actual now.
There are exist some options like 'Full Screen Display -> Full Screen Gestures' where navigation bar is hidden but all this methods returns that he is present.

I suggest you to use this way to check size of system views. In onCreate method:

ViewCompat.setOnApplyWindowInsetsListener(findViewById(android.R.id.content), 
  (v, insets) -> { 
      int navigationBarHeight = insets.getSystemWindowInsetBottom();
      return insets;
  });
Suntan answered 29/8, 2019 at 12:6 Comment(0)
P
1

Other answers don't help me. But it's quite useful to know if navigation bar is shown, especially after Android P/Q, where user can swipe it out of screen. I've encounter this article https://blog.stylingandroid.com/gesture-navigation-window-insets/ and made such method

fun hasNavBar(activity: Activity): Boolean {
    val temporaryHidden = activity.window.decorView.visibility and View.SYSTEM_UI_FLAG_HIDE_NAVIGATION != 0
    if (temporaryHidden) return false
    val decorView = activity.window.decorView
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        decorView.rootWindowInsets?.let{
            return it.stableInsetBottom != 0
        }
    }
    return true
}
Picnic answered 4/7, 2019 at 19:4 Comment(3)
Please edit your answer to explain the code you've provided. Putting an explanation of the code directly in your answer will help future readers to understand what it does and how.Thrasonical
What is unclear in my answer? First part is checking HIDE_NAVIGATION ui visibility flag of activity window decorView and second is checking bottom inset of those decorView. Works from 23 API.Picnic
Nothing is particularly unclear, but it is generally encouraged to comment or explain code snippets on this site. OP made the question because they don't know how to solve the problem, which might mean that they have never seen some or any of the individual statements in your code (if they had seen it before, they probably could have solved the problem themselves). So there is value in going through and adding a comment for each chunk of your code. The explanation can go into the answer itself, not a comment, please.Thrasonical
H
1

The point of finding the soft navigation enabled or not is to determine what is the size of the window provided and to set the layout according to.

There is a very powerful tool called decorView that sets the window from before so as to directly implement methods under it. It can be written like this:

val decorView = window.decorView
decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE

Just start writing methods under it whichever you want to make inside the frame.

Found this one working after trying a ton of methods and tricks and this one is the only one working.

Hypothetical answered 23/8, 2021 at 15:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.