37

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

Raghu Mudem
  • 6,218
  • 12
  • 42
  • 62

7 Answers7

73

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

Raghu Mudem
  • 6,218
  • 12
  • 42
  • 62
  • 1
    This code works but instead of 'android' use '"android"' – MattButtMatt Jun 15 '16 at 19:22
  • 11
    Returns `false` in both cases. – CoolMind Feb 08 '17 at 13:21
  • 4
    Note that this always returns `false` on the Android emulators. See [this commit message](https://github.com/android/platform_frameworks_base/commit/0c4ccff36930ff4f0292b94ad51e164c9fa060a3) for an explanation. – Sam May 24 '17 at 23:49
  • 4
    Despite not working on the emulators, this is very close to [what the Android source code does](https://github.com/android/platform_frameworks_base/blob/124460817263f5449d59a4345ab24dde82645bd9/services/core/java/com/android/server/policy/PhoneWindowManager.java#L1991), so it should be pretty accurate on real devices. – Sam May 24 '17 at 23:59
  • checked in one plus 3 enabling both its always returns false. – Muneef M Dec 29 '17 at 07:53
  • How can we get bar height in PX or DP? – Hannan May 14 '18 at 08:14
  • Hello @MuhammadHannan, you could use the following method to get the any resource size in pixel. resources.getDimensionPixelSize(id); – Raghu Mudem May 30 '18 at 14:14
  • 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: https://stackoverflow.com/questions/20264268/how-do-i-get-the-height-and-width-of-the-android-navigation-bar-programmatically/59302624#59302624 . We can get real height of the navigation bar displaying on screen to detect it visible or not. – Anh Duy Dec 12 '19 at 11:24
25

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));
    }
IshRoid
  • 3,408
  • 2
  • 22
  • 36
  • Thanks for your response. I am looking to determine soft navigation bar presence. – Raghu Mudem Mar 11 '15 at 10:12
  • 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 http://www.gsmarena.com/asus_zenfone_5-pictures-5952.php – Raghu Mudem Mar 11 '15 at 10:18
  • Thank you for such great information, because of this type of devices you can try my second solution (look updated answer) – IshRoid Mar 11 '15 at 10:19
  • Thanks man. Seems it will work for me. I tested on two available devices as expected. – Raghu Mudem Mar 11 '15 at 10:28
  • Enjoy programming, if this answer help you , then accepting answer or upvote will be very appreciated :-) – IshRoid Mar 11 '15 at 10:29
  • For me only the first solution works. The second failed on nexus 5 – Thomas Cirksena Mar 10 '17 at 07:35
  • No idea why `hasBackKey` equals true on Sony Xperia Z3. Which make the second solution fail. – Hamzeh Soboh Mar 13 '17 at 10:01
  • Thanks bro you saved my life ! – PriyankaChauhan Jun 23 '17 at 08:27
  • This is not a correct answer! This solution only checks if device has physical navigation button, but there are also devices where both exists. – Serdar Samancıoğlu Apr 25 '19 at 08:33
  • 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. https://stackoverflow.com/questions/20264268/how-do-i-get-the-height-and-width-of-the-android-navigation-bar-programmatically/59302624#59302624 – Anh Duy Dec 12 '19 at 11:29
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;
}
Noman Rafique
  • 3,392
  • 23
  • 29
9

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;
    }
}
Sam
  • 35,545
  • 30
  • 157
  • 202
  • 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. – weaknespase Nov 21 '17 at 18:01
  • it is working for me but I am getting a warning about I can get a null pointer exception. Is it safe to use? – JavierSegoviaCordoba Feb 01 '18 at 21:48
  • @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. – Sam Feb 01 '18 at 21:55
  • @Sam 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. – JavierSegoviaCordoba Feb 01 '18 at 21:59
  • @Dahnark Oh, I see. Don't know sorry. – Sam Feb 01 '18 at 22:06
  • Is there any way to get Notification bar is visible or not in Android P? – Nik Mar 01 '19 at 08:26
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;

}
Riccardo
  • 37
  • 4
  • 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. – Gerrit Humberg Dec 08 '20 at 14:27
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;
  });
Dmitriy Puchkov
  • 1,446
  • 13
  • 35
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
}
parmakli
  • 11
  • 2
  • 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. – mypetlion Jul 04 '19 at 19:27
  • 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. – parmakli Jul 07 '19 at 08:56
  • 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. – mypetlion Jul 08 '19 at 16:54