Search code examples
javaandroiduinavigationbarfullscreen

Hide navigation bar without delay in Android


I have a simple application which loads in the background an ImageView.

<ImageView
    android:id="@+id/splashimage"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="centerCrop"
    android:adjustViewBounds="true"
    android:src="@drawable/logo"
    android:visibility="gone"/>

I am trying to load the image in full screen mode, meaning no navigation bar and no app bar. I have been able to remove te appbar from the very first moment that I run the application by setting the following flag:

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

Nevertheless, when I try to do the same with the navigation bar at the bottom of the application, there is some delay before getting removed. Therefore, the behaviour is a bit weird. What it happens is that first the navigation bar is shown, next it is hidden and shows a white background, and only after a second the image fills the gap. I attach a photo to try to reproduce the situation as clear as possible.

My question is, how could I show the image covering that space from the beginning rather than following the 3 steps behaviour mentioned below?

To hide the navigation bar this code is used;

        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN |
            View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
            View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);

enter image description here


Solution

  • After following Bruno s comment I have updated my code and solve the error:

     getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_IMMERSIVE
                // Set the content to appear under the system bars so that the
                // content doesn't resize when the system bars hide and show.
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                // Hide the nav bar and status bar
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
    

    Note that I did not want to remove completely the appbar. Therefore I did not include the following line | View.SYSTEM_UI_FLAG_FULLSCREEN); at the end.

    Update

    Note that if you want your layout to appear under the appbar and not under it, this line needs to be removed

                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE