Hiding the status and navigation bars immediately in app launch
Asked Answered
N

3

5

I was wondering if there is a way to hide an Android device's soft keys (status and navigation bars) right when an app launches? Take, for instance, a game that initially displays a black screen while hiding the soft keys in runtime prior to the brand, title, and main menu screen.

I included the following code snippet in my MainActivity, and when the app launches, I initially see a white screen with the soft keys in runtime for about a second and then the softkeys later disappear when MainActivity's thread finishes running:

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if (hasFocus) {
        getWindow().getDecorView().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_IMMERSIVE_STICKY);
    }
}

Thanks a bunch.

* UPDATE AS OF 1/19/2017: * After including:

<item name="android:windowFullscreen">true</item>

... into the app's theme, only the status bar hides on time as opposed to the navigation bar. My second post - Hiding the navigation bar immediately on app launch

Nolte answered 18/1, 2017 at 20:36 Comment(1)
Maybe it's possible in your case to make use of this solution...Booboo
P
5

have a look into your styles.xml and add the item android:windowFullscreen.

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowFullscreen">true</item>
</style>

I hope that helps.

Ben

Pageantry answered 18/1, 2017 at 20:47 Comment(3)
Strange, I tried on my device and it worked immediately. Good luck debugging.Pageantry
For both the status AND navigation bars? What device did you use? Because, I tried the code above for my device that runs Android 6.0, and only the status bar hides appropriately.Nolte
Here's an update of where I stand: #41748514Nolte
J
2

The trick is to set immersive mode flags before super.onCreate(savedInstanceState) in onCreate() method of all your fullscreen activities.

@Override
 protected void onCreate(Bundle savedInstanceState) {
     hideSystemUI(getWindow());   //this hides NavigationBar before showing the activity

     super.onCreate(savedInstanceState);

     setContentView(R.layout.activity_main); 
     ...
}

also don't forget to add following code in all your fullscreen activities

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    hideSystemUI(getWindow());
}

public static void hideSystemUI(Window window) {
    window.getDecorView().setSystemUiVisibility(
            View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LOW_PROFILE
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
    );
}

And also add the following lines in your activity theme in styles.xml

<style name="FullScreenAppTheme" parent="Theme.AppCompat.Light.NoActionBar">
     ...

     <item name="android:windowFullscreen">true</item>
     <item name="android:windowTranslucentNavigation">true</item>
</style>

This functionality should work for android api 19 and above. Tested on api 21 and above. Launcher activity may not hide navigation bar instantly but all other fullscreen activities started from the launcher activity will surely not show navigation bar while opening.

Hope it helps!

Jackquelin answered 26/3, 2018 at 9:13 Comment(0)
U
0

There are several ways (described here - https://developer.android.com/training/system-ui/status.html):

1) Make another call to setSystemUiVisibility() in your activity's onCreate() method.

2) For Android 4.0 and lower. You can change your theme in XML layout like this: ... android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen" > ...

3) For Android 4.0 and lower. Hide the status bar in your Activity onCreate(Bundle savedInstanceState) method. Here's an example from aforementioned site:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
    if (Build.VERSION.SDK_INT < 16) {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }
Urology answered 18/1, 2017 at 20:59 Comment(3)
I'm currently resorting to immersive sticky mode for displaying the app in full screen, so I'll be using Android 4.4 as the minimum... However, were you able to ever find out how to hide both the status and navigation bar at the same time at startup like a game or YouTube's full screen mode? Here's a related question I posted recently: #41748514Nolte
Try adding flag View.SYSTEM_UI_FLAG_HIDE_NAVIGATION to setSystemUiVisibility. However, nav bar will reappear if you touch anywhere.Urology
You may want to use SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION instead to make the content of your app appear behind the nav bar.Urology

© 2022 - 2024 — McMap. All rights reserved.