Hiding the navigation bar immediately on app launch
Asked Answered
T

3

3

Based on the following code snippets, I was able to hide the status bar right when the app launches, but not the navigation bar (the bar consisting of the back, home, and task manager buttons) since it hides later after MainActivity's thread finishes loading up:

Here's the manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="dpark.sample2">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

... here's the styles:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowFullscreen">true</item>
    </style>

</resources>

... and MainActivity:

public class MainActivity extends AppCompatActivity {

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

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

... How do I hide both bars from the start at the same time like a game or YouTube's full screen mode?

Trudi answered 19/1, 2017 at 17:26 Comment(2)
Any luck on this ? I have the same requirement.Malan
Is this really impossible to achieve?Rutty
C
1

Move this line of code to onCreate()-

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);
Cerberus answered 19/1, 2017 at 19:20 Comment(1)
Tried that already. I even put it before setContentView in onCreate. The delay of the navigation bar being hidden is even more noticeable on devices with a slower RAM such as a 512MB virtual emulator. I just wish <item name="android:windowFullscreen">true</item> hid both bars at the same time to make my life easier...Trudi
B
0

Im trying to solve this for my splash screen and no luck so far. But you can make things look better if you add this to your style theme:

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

Navigation will be still visible, but translucent. This is how my theme looks like now:

<style name="SplashTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    <item name="android:windowBackground">@drawable/ps_splash_screen</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowTranslucentNavigation">true</item>
    <item name="android:windowContentOverlay">@null</item>
</style>

Good luck.

Buckboard answered 25/9, 2019 at 9:3 Comment(0)
S
0

Try this in your onCreate

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
                    WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        }
Schutt answered 25/9, 2019 at 9:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.