How to set fullscreen in Android R?

45.3k Views Asked by At

I need to put a screen in fullscreen in my app. For this I am using this code:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    requestWindowFeature(Window.FEATURE_NO_TITLE)
    window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN)
    setContentView(R.layout.activity_camera_photo)

However, the WindowManager.LayoutParams.FLAG_FULLSCREEN flag is deprecated.

My app supports Android Lollipop (API 21) to Android R (API 30). What is the correct way to make a screen go fullscreen?

7

There are 7 best solutions below

7
Saurabh Thorat On

For API >= 30, use WindowInsetsController.hide():

window.insetsController.hide(WindowInsets.Type.statusBars())
14
Andriy D. On

KOTLIN

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.layout_container)
    @Suppress("DEPRECATION")
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        window.insetsController?.hide(WindowInsets.Type.statusBars())
    } else {
        window.setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN
        )
    }
}

if this doesn't help, try to remove android:fitsSystemWindows="true" in the layout file

JAVA

class Activity extends AppCompatActivity {

@Override
@SuppressWarnings("DEPRECATION")
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_container);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        final WindowInsetsController insetsController = getWindow().getInsetsController();
        if (insetsController != null) {
            insetsController.hide(WindowInsets.Type.statusBars());
        }
    } else {
        getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN
        );
    }
}
}
0
Mandarin Smell On

I had problem like user924

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.WindowInsetsController com.android.internal.policy.DecorView.getWindowInsetsController()' on a null object reference

I could fix this problem by adding full screen setting code after setContentView. Also, usually, full screen will be screen without not only status bar, but also navigation bar too. Furthermore, just hide() method isn't enough. If we put only this line, when we swipe down screen to see status bar, it comes down, but never goes up again. By setting systemBarBehavior, we can make status bar and navigation bar appear temporarily only when we swipe just like full screen what we know.

setContentView(R.layout.YOUR_LAYOUT)

//Set full screen after setting layout content
@Suppress("DEPRECATION")
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
    val controller = window.insetsController

    if(controller != null) {
        controller.hide(WindowInsets.Type.statusBars() or WindowInsets.Type.navigationBars())
        controller.systemBarsBehavior = WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
    }
} else {
    window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
}
0
metvsk On
fullScreenButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {

                    WindowInsetsController controller = getWindow().getInsetsController();
                    //BEFORE TOGGLE
//                    System.out.println(controller.getSystemBarsAppearance());
//                    System.out.println(controller.getSystemBarsBehavior());
                    if(controller != null) {
                        if (controller.getSystemBarsBehavior() == 0) {
                            controller.hide(WindowInsets.Type.statusBars() | WindowInsets.Type.navigationBars());
                            controller.setSystemBarsBehavior(WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
                        } else {
                            controller.show(WindowInsets.Type.statusBars() | WindowInsets.Type.navigationBars());
                            controller.setSystemBarsBehavior(WindowInsetsController.BEHAVIOR_SHOW_BARS_BY_TOUCH);
                        }
                    }
                } else {
                    WindowManager.LayoutParams attrs = getWindow().getAttributes();
                    attrs.flags ^= WindowManager.LayoutParams.FLAG_FULLSCREEN;
                    getWindow().setAttributes(attrs);
                }
                //AFTER TOGGLE
                //                    System.out.println(controller.getSystemBarsAppearance());
//                    System.out.println(controller.getSystemBarsBehavior());
            }
        });
1
Casual Passerby On

The code runs on real phones with Android 4.0 (API 14) to 10 (API 29) and on SDK phone emulator with Android R (API 30).

Write the theme for splash activity in style resources.

 <!--Splash screen theme-->
  <style name="SplashTheme" parent="@style/Theme.AppCompat.NoActionBar">
  <item name="android:windowFullscreen">true</item>
  <item name="android:windowBackground">@color/colorSplashBackground</item>
  </style>

It's enough. No code is need to hide bar for splash activity.

Main activity.

Use the theme.

<!-- Base application theme. -->
    <style name="myAppTheme" parent="@style/Theme.AppCompat.NoActionBar">
    <!-- Customize your theme here. -->
    </style>

Write code in MainActivity class.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate( savedInstanceState );
    // Hide bar when you want. For example hide bar in landscape only
    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
        hideStatusBar_AllVersions();
    }
    setContentView( R.layout.activity_main );
    // Add your code
    }

Implement methods.

@SuppressWarnings("deprecation")
void hideStatusBar_Deprecated() {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {  // < 16
        getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
    } else {  // 16...29
        View decorView = getWindow().getDecorView();
        decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);

        ActionBar ab = getSupportActionBar();
        if (ab != null) { ab.hide(); }

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

@TargetApi(Build.VERSION_CODES.R) // >= 30
void hideStatusBar_Actual() {
    View decorView = getWindow().getDecorView();
    decorView.getWindowInsetsController().hide(WindowInsets.Type.statusBars());
}

void hideStatusBar_AllVersions(){
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
        hideStatusBar_Deprecated();
    } else {
        hideStatusBar_Actual();
    }
}
0
androidmalin On
   override fun onAttachedToWindow() {
    super.onAttachedToWindow()
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        val controller = window.insetsController
        if (controller != null) {
            controller.hide(WindowInsets.Type.statusBars() or WindowInsets.Type.navigationBars())
            controller.systemBarsBehavior =
                WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
        }
    }
}

@Suppress("DEPRECATION")
private fun makeFullScreen() {
    // Remove Title
    requestWindowFeature(Window.FEATURE_NO_TITLE)
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
        window.setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN
        )
    }
    // Hide the toolbar
    supportActionBar?.hide()
}
1
pragneshReddy Nandyala On
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {     
 window.insetsController!!.hide(android.R.style.Theme_NoTitleBar_Fullscreen)
}