How to set fullscreen in Android R?
Asked Answered
B

7

48

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?

Biyearly answered 10/7, 2020 at 13:11 Comment(0)
T
71

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
        );
    }
}
}
Turmel answered 15/7, 2020 at 9:29 Comment(14)
What is the type of window object here and how should it be initialized in Java? @Andriy D.Gabe
@andriy D. final WindowInsetsController insetsController = getWindow().getInsetsController(); this line is cause of javaNullPointerException. how to solve it?Elouise
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.WindowInsetsController com.android.internal.policy.DecorView.getWindowInsetsController()' on a null object referenceCineraria
@Cineraria could you provide your activity/fragment, please?Turmel
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?Inject
@SaeidZ could you provide your activity/fragment, please?Turmel
@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().Inject
@SaeidZ yes, exactly. I've updated the answer, thanks for the comment.Turmel
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(); }Cineraria
and it's wrong that after DataBindingUtil.setContentView(this, R.layout.activity_layout) it won't be null!Cineraria
@SaeidZ no, you aren't right. getWindow().getInsetsController() won't work after setContentView in onCreate, only in onAttachedToWindowCineraria
@Cineraria 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.Inject
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 hiddenHarvell
Use the compat version of WindowInsets.Type to get rid of the api checkThirzia
H
17

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)
}
Harv answered 12/1, 2021 at 19:0 Comment(1)
The only solution that worked for me that behaves just like the deprecated FLAG_FULLSCREEN. Placed it in onAttachedToWindow() as it makes more sense to call it there.Yorktown
E
11

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

window.insetsController.hide(WindowInsets.Type.statusBars())
Econah answered 10/7, 2020 at 14:14 Comment(7)
this solution will need a condition on version sdkIncidentally
What is the type of window object here and how should it be initialized in Java?@Incidentally @Saurabh ThoratGabe
@RAWNAKYAZDANI For Java, use getWindow() in your activityEconah
@SaurabhThorat hide() is non static method and i can not use it static onCreate method. how to solve it?Elouise
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.WindowInsetsController com.android.internal.policy.DecorView.getWindowInsetsController()' on a null object referenceCineraria
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(); }Cineraria
window.insetsController?.hide(WindowInsets.Type.statusBars()) [For Kotlin, works]Azores
C
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();
    }
}
Chas answered 27/7, 2021 at 9:3 Comment(1)
Not working for Android 30 emulatorPhotocomposition
C
1
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {     
 window.insetsController!!.hide(android.R.style.Theme_NoTitleBar_Fullscreen)
}
Cimon answered 14/1, 2022 at 12:53 Comment(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?Robinet
M
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());
            }
        });
Manche answered 28/5, 2021 at 14:34 Comment(0)
H
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()
}
Henriques answered 8/12, 2021 at 10:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.