Home > OS >  Android Studio Full screen deprecated
Android Studio Full screen deprecated

Time:10-10

so I created an app, but unfortunately, I stumbled upon a small problem. My full-screen code is deprecated and the status bar (but it's only a black line, you can't see batter percentage and clock until you click on that black line) is showing on some devices while on Samsung Note 8 everything is okay. I think the problem is with API but I can't find how to solve this problem. Do you think you can help me?

This is theme.xml, the same code is on the dark and light theme.

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Myapp" parent="Theme.MaterialComponents.Light.NoActionBar">
        <item name="android:forceDarkAllowed" tools:targetApi="q">false</item>



        <!-- Primary brand color. -->
        <item name="colorPrimary">@android:color/holo_red_dark</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@android:color/holo_red_dark</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->


    </style>

    <style name="Myapp.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>

    </style>

    <style name="Myapp.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="Myapp.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>

Java class code

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);

I tried adding this code but it didn't help

<item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>

CodePudding user response:

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

CodePudding user response:

You can use AndroidX AppCompat library to make it easier to handle full-screen on every api level.

First, you need to request the framework not fit the content view

WindowCompat.setDecorFitsSystemWindows(getWindow(), false);

Then create WindowInsetsControllerCompat to hide or show the system bars

WindowInsetsControllerCompat windowInsetsController = new WindowInsetsControllerCompat(getWindow(), getWindow().getDecorView());

// Hide (full-screen)
windowInsetsController.hide(WindowInsetsCompat.Type.systemBars());

If you want to use immersive mode, you just need to call windowInsetsController.setSystemBarsBehavior(BEHAVIOR_SHOW_BARS_BY_SWIPE);

Full code:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.id.activity_main);
        WindowCompat.setDecorFitsSystemWindows(getWindow(), false);

        WindowInsetsControllerCompat windowInsetsController = new WindowInsetsControllerCompat(getWindow(), getWindow().getDecorView());

        // Hide system bars
        windowInsetsController.hide(WindowInsetsCompat.Type.systemBars());
    }
}

You can learn more in this video

  • Related