Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: detect navigation bar visibility

Tags:

android

How can I detect the presence of the navigation bar and hide it?

In my onCreate() I call hideNavigationBar() method to hide the navigation bar, then I register a listener to hide the navigation bar every time it becomes visible when the user touches anywhere on the screen as reported by the documentations. When the navigation bar becomes visible after a touch event the hideNavigationBar() method is called again by the listener, but it has not effect, the bar is still visible.

This is my onCreated() method:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        hideNavigationBar();

        View decorView = getWindow().getDecorView();
        decorView.setOnSystemUiVisibilityChangeListener
                (new View.OnSystemUiVisibilityChangeListener() {
                    @Override
                    public void onSystemUiVisibilityChange(int visibility) {
                        if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
                            Toast.makeText(getApplicationContext(), "Visible", Toast.LENGTH_SHORT).show();
                            hideNavigationBar();
                        } else {
                            Toast.makeText(getApplicationContext(), "Not visible", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

and this is my hideNavigationBar() method:

 private void hideNavigationBar() {

        Toast.makeText(getApplicationContext(), "hideNavigationBar()", Toast.LENGTH_SHORT).show();

        View decorView = getWindow().getDecorView();
        int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_FULLSCREEN;
        decorView.setSystemUiVisibility(uiOptions);
    }

How can I hide the navigation bar every time it becomes visible?

Thanks

like image 697
Stephan Avatar asked Apr 21 '15 14:04

Stephan


People also ask

How do I know if my navigation bar is visible Android?

There's no reliable way to check for a navigation bar. Using KeyCharacterMap. deviceHasKey you can check if certain physical keys are present on the device, but this information is not very useful since devices with physical keys can still have a navigation bar.

How do I show hidden navigation bar?

Touch “Settings” -> “Display” -> “Navigation bar” -> “Buttons” -> “Button layout”. Choose the pattern in “Hide navigation bar” -> When the app opens, the navigation bar will be automatically hidden and you can swipe up from the bottom corner of the screen to show it.

Why does my navigation bar disappear?

If you are using a full screen app the navigation bar will automatically hide so that you can make the most of your screen. It will appear when you swipe down from the top of the screen.

What is a status bar in Android?

Status bar (or notification bar) is an interface element at the top of the screen on Android devices that displays the notification icons, minimized notifications, battery information, device time, and other system status details.


2 Answers

you could add this code to your activity's onCreate() method:

      View decorView = getWindow().getDecorView();
    decorView.setOnSystemUiVisibilityChangeListener
            (new View.OnSystemUiVisibilityChangeListener() {
                @Override
                public void onSystemUiVisibilityChange(int visibility) {
                    if ((visibility & View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) == 0) {                
                        // TODO: The navigation bar is visible. Make any desired
                        // adjustments to your UI, such as showing the action bar or
                        // other navigational controls.
                        hideNavigationBar() 

                    } else {
                        // TODO: The navigation bar is NOT visible. Make any desired
                        // adjustments to your UI, such as hiding the action bar or
                        // other navigational controls.
                    }
                }
            });

It's generally good practice to keep your UI in sync with changes in system bar visibility. For example, you could use this listener to hide and show the action bar in concert with the status bar hiding and showing.Android-Responding to UI Visibility Changes

like image 121
joe Avatar answered Sep 29 '22 21:09

joe


Do this:

boolean hasMenuKey = ViewConfiguration.get(context).hasPermanentMenuKey();
boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);

if(!hasMenuKey && !hasBackKey) {
    // Do whatever you need to do, this device has a navigation bar
}

Original answer Check for navigation bar

like image 45
Vinicius DSL Avatar answered Sep 29 '22 21:09

Vinicius DSL