mystery
mystery

Reputation: 153

Hiding navigation bar without rooting on android

I want to make fullscreen application for android tablets and i try to hide navigation bar and for 4.0+. I read similar questions and answers to it but what i found are dont work for me. If it is not possible to hide navigation bar forever, how can i hide it and after make it visible with slide from bottom? I saw in some games and i think this is possible. So far i tried this and at first it hides navigation bar but when i touched the screen navigation bar become visible again and i dont want it.

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

and i also tried this but again doest work for me

decorView.setOnSystemUiVisibilityChangeListener
            (new View.OnSystemUiVisibilityChangeListener() {
        @Override
        public void onSystemUiVisibilityChange(int visibility) {
            // Note that system bars will only be "visible" if none of the
            // LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set.
            if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
                int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_FULLSCREEN;
          decorView.setSystemUiVisibility(uiOptions);


            } else {
                int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_FULLSCREEN;
          decorView.setSystemUiVisibility(uiOptions);
            }
        }
    });

How can i hide it and make it visible with sliding from bottom or hide forever? Please give me an advice. Thanks in advance

Upvotes: 1

Views: 1955

Answers (1)

paulscode
paulscode

Reputation: 1069

I believe what you are describing that you have seen in some games is called "Immersive Mode". The navigation bar disappears completely, and a gesture from the edge of the screen brings it back. This feature is available starting at Android version 4.4.

Before Android 4.4, since Android version 4.0, you can use the "Hide Navigation" flag, which will temporarily hide the navigation bar. The problem with this mode is that any interaction with the screen will cause the navigation bar to reappear. This of course isn't very useful for apps that require a lot of interaction with the screen, like most games.

I typically use the following functions for moving in and out of immersive mode:

private void hideSystemUI() {
    mDecorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_FULLSCREEN
            | View.SYSTEM_UI_FLAG_LOW_PROFILE
            | View.SYSTEM_UI_FLAG_IMMERSIVE); // This flag only for 4.4+
}

private void showSystemUI() {
    mDecorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}

Then in my onCreate, I set mDecorView and call hideSystemUI:

    mDecorView = getWindow().getDecorView();
    hideSystemUI();

The code is similar to what you have though, so this may not help. There is a detailed video on the subject, including much more detailed source code examples, here: https://www.youtube.com/watch?v=cBi8fjv90E4

Upvotes: 1

Related Questions