DaveNOTDavid
DaveNOTDavid

Reputation: 1803

Hiding the status and navigation bars immediately in app launch

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

Upvotes: 5

Views: 10966

Answers (3)

KrzyShzy
KrzyShzy

Reputation: 156

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!

Upvotes: 2

Reaper
Reaper

Reputation: 757

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);
    }

Upvotes: 0

bedawi
bedawi

Reputation: 231

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

Upvotes: 5

Related Questions