简体   繁体   中英

Immersive Mode Android Studio

I want the game that I'm making to run in immersive mode, but android studio doesn't recognize the flag immersive mode because I set my minimum API to 16, and I know immersive mode was added only in KitKat which is later on. Is there any way to have my app run in immersive mode without changing my minimum API?

Yes, it is possible, but of course this immersive mode will be only working on devices with KitKat and higher. This, what is weird on your side, is fact, that basing on your words, you cannot even get these flags like this:

View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;

(or part of them). If it is this way, then it is looking, that your compileSdkVersion is lower, than it should be. On start I would advise you to update compileSdkVersion to 22 (and also make targetSdkVersion also 22) (both things you will find in build.gradle)

When you will do this, and you would like to use these flags please in places, where you want to use immersive mode add conditions, that will be looking like this:

if (Build.VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
    int UI_OPTIONS = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
    getWindow().getDecorView().setSystemUiVisibility(UI_OPTIONS);
}

Then it should not mess on older OS.

( UPDATE : 2nd block of code was updated)

Just Paste this Function in Activity Class and you are done

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

    if (hasFocus) {

        View decorView = getWindow().getDecorView();
        decorView.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);
    }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM