46

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?

Paulo Boaventura
  • 1,365
  • 1
  • 9
  • 29
Vitor Ferreira
  • 1,075
  • 1
  • 14
  • 28

7 Answers7

67

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
        );
    }
}
}
Boris Strandjev
  • 46,145
  • 15
  • 108
  • 135
Andriy D.
  • 1,801
  • 12
  • 19
  • What is the type of **window** object here and how should it be initialized in **Java**? @Andriy D. – Rawnak Yazdani Aug 12 '20 at 12:53
  • @andriy D. final WindowInsetsController insetsController = getWindow().getInsetsController(); this line is cause of javaNullPointerException. how to solve it? – Amir Ehsani Nov 23 '20 at 22:20
  • 6
    `java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.WindowInsetsController com.android.internal.policy.DecorView.getWindowInsetsController()' on a null object reference` – user924 Nov 30 '20 at 16:09
  • @user924 could you provide your activity/fragment, please? – Andriy D. Nov 30 '20 at 16:12
  • Have error: Attempt to invoke virtual method 'android.view.WindowInsetsController com.android.internal.policy.DecorView.getWindowInsetsController()' on a null object reference.In oder words getWindow() retun null.How to solve it? – Saeid Z Dec 02 '20 at 18:21
  • @SaeidZ could you provide your activity/fragment, please? – Andriy D. Dec 03 '20 at 09:21
  • 1
    @AndriyD. My activity is like as you write on your answer. I added getWindow().getInsetsController(); before the setContentView(R.layout.main_layout); But when i call getWindow() after setContentView work fine and don't return null. I guess we have to call getWindow().getInsetsController(); afetr setContentView(). – Saeid Z Dec 03 '20 at 13:58
  • @SaeidZ yes, exactly. I've updated the answer, thanks for the comment. – Andriy D. Dec 03 '20 at 14:09
  • 2
    you can't use it in `onCreate` t it should be in `onAttachedToWindow`. Check `public @Nullable WindowInsetsController getWindowInsetsController() { if (mAttachInfo != null) { return mAttachInfo.mViewRootImpl.getInsetsController(); }` – user924 Dec 10 '20 at 18:06
  • and it's wrong that after `DataBindingUtil.setContentView(this, R.layout.activity_layout)` it won't be `null`! – user924 Dec 10 '20 at 18:07
  • @SaeidZ no, you aren't right. `getWindow().getInsetsController()` won't work after `setContentView` in `onCreate`, only in `onAttachedToWindow` – user924 Dec 10 '20 at 18:08
  • @user924 Thanks. I don't know it work or no, but when use it before setContentView(), that return null. I think in newer androids (api 30) insetsController.hide(WindowInsets.Type.statusBars()); method is ineffective and only set full screen style for activity is enough to make that full screen. – Saeid Z Dec 14 '20 at 18:17
  • Incorrect, this is just a equivalent of `getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN)` not `getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)` they behave different as when you swipe down to screen the status bar will remain unlike `WindowManager.LayoutParams.FLAG_FULLSCREEN` where it goes back hidden – Bitwise DEVS May 02 '21 at 10:48
  • 1
    Use the compat version of WindowInsets.Type to get rid of the api check – arekolek Oct 21 '21 at 16:59
15

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)
}
Mandarin Smell
  • 391
  • 2
  • 17
10

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

window.insetsController.hide(WindowInsets.Type.statusBars())
Saurabh Thorat
  • 18,131
  • 5
  • 53
  • 70
  • 2
    this solution will need a condition on version sdk – Mimouni Aug 08 '20 at 14:09
  • What is the type of **window** object here and how should it be initialized in **Java**?@Mimouni @Saurabh Thorat – Rawnak Yazdani Aug 12 '20 at 12:51
  • 1
    @RAWNAKYAZDANI For Java, use `getWindow()` in your activity – Saurabh Thorat Aug 12 '20 at 13:06
  • @SaurabhThorat hide() is non static method and i can not use it static onCreate method. how to solve it? – Amir Ehsani Nov 23 '20 at 22:21
  • 2
    `java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.WindowInsetsController com.android.internal.policy.DecorView.getWindowInsetsController()' on a null object reference` – user924 Nov 30 '20 at 16:09
  • remember you can't use it in `onCreate` t it should be in `onAttachedToWindow`. Check `public @Nullable WindowInsetsController getWindowInsetsController() { if (mAttachInfo != null) { return mAttachInfo.mViewRootImpl.getInsetsController(); }` – user924 Dec 10 '20 at 18:06
  • window.insetsController?.hide(WindowInsets.Type.statusBars()) [For Kotlin, works] – Dev Gurung Jan 01 '21 at 02:24
4

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();
    }
}
1
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {     
 window.insetsController!!.hide(android.R.style.Theme_NoTitleBar_Fullscreen)
}
Ihor Konovalenko
  • 1,298
  • 2
  • 16
  • 21
  • 1
    Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Jan 16 '22 at 00:31
0
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());
            }
        });
Dharman
  • 30,962
  • 25
  • 85
  • 135
metvsk
  • 189
  • 2
  • 4
0
   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()
}
androidmalin
  • 899
  • 1
  • 8
  • 12