How to remove top status bar black background
Asked Answered
I

7

20

I'd like to create a TRUE fullscreen activity, but there is always a black status bar on screen top. Android 9.0.

I've tried almost all I can find with Google and existing Apps with similar jobs. Manifest, code, style, AS sample fullscreen activity, all were tried.

styles.xml:

    <style name="AppThemeA" parent="@style/Theme.AppCompat.Light.NoActionBar">
        <item name="android:statusBarColor">@android:color/transparent</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowActionBar">false</item>
        <item name="android:windowFullscreen">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowTranslucentNavigation">true</item>
    </style>

Manifest:

<activity android:name=".ScreenActivity" android:theme="@style/AppThemeA" />

Layout:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/rootLayout"
        android:fitsSystemWindows="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

Kotlin (commented lines are tried and failed):

class ScreenActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        //setTheme(R.style.AppThemeDetector)
        super.onCreate(savedInstanceState)


        //window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
        //window.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
        //window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or View.SYSTEM_UI_FLAG_FULLSCREEN

        /*
        window.decorView.systemUiVisibility = (
                View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        or View.SYSTEM_UI_FLAG_FULLSCREEN
                )*/

        setContentView(R.layout.activity_screen_detector)
        ...
    }
}

The expected result:
expected

What I actually got:
actual

[[[[ Solution ]]]]

Found what caused this. You should set the App to FullScreen App in Settings -> Display.

https://www.gottabemobile.com/how-to-enable-full-screen-apps-on-galaxy-s10

So fixed. Thanks for all your help.

Inmate answered 29/5, 2019 at 5:34 Comment(4)
You can comment on each individual answer to say if it did not work, rather than updating the question.Brice
Try this: #26493022Attainable
try this:#39342318Courtland
try this Link It will help you lot.Boldface
S
24

I had a problem with displaying content through the notch or the cutout area. Found this in the docs:

LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES - Content renders into the cutout area in both portrait and landscape modes.

Key thing for me was this line in the activity style:

// Important to draw through the cutouts
<item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item> 

I wanted to show an image in immersive mode, and when I click it, the system UI (status & navigation bars) should show up.

Here is my complete solution:

1- Methods to show/hide system UI in the Activity

private fun hideSystemUI() {
    sysUIHidden = true
    window.decorView.systemUiVisibility = (
            View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
            or View.SYSTEM_UI_FLAG_LAYOUT_STABLE 
            // Hide the nav bar and status bar
            or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // Hide nav bar
            or View.SYSTEM_UI_FLAG_FULLSCREEN // Hide status bar
            )
}


private fun showSystemUI() {
    sysUIHidden = false
    window.decorView.systemUiVisibility = (
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            // Set the content to appear under the system bars so that the
            // content doesn't resize when the system bars hide and show.
            or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION // layout Behind nav bar
            or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN // layout Behind status bar
            )
}

2- Make sure this in the root view of your xml layout

android:fitsSystemWindows="false"

3- The Style for the Full screen Activity will give status/navigation bars a semi transparent background when they show up:

<style name="FullscreenTheme" parent="AppTheme">
    <item name="android:actionBarStyle">@style/FullscreenActionBarStyle</item>
    <item name="android:windowActionBarOverlay">true</item>
    <item name="android:windowBackground">@null</item>
    <item name="metaButtonBarStyle">?android:attr/buttonBarStyle</item>
    <item name="metaButtonBarButtonStyle">?android:attr/buttonBarButtonStyle</item>
    <item name="android:statusBarColor">#50000000</item>
    <item name="android:navigationBarColor">#50000000</item>
    // Important to draw behind cutouts
    <item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item> 
</style>

<style name="FullscreenActionBarStyle" parent="Widget.AppCompat.ActionBar">
    <item name="android:background">@color/sysTransparent</item>
</style>
Springtail answered 23/4, 2020 at 19:12 Comment(1)
Thanks a ton, I am using immersive mode, it works well with all devices except Samsung that always display a black status bar. windowLayoutInDisplayCutoutMode helps to solve it, crazy android :DErvinervine
S
20

Add this method in your activity

public static void hideSystemUI() {
    getWindow().getDecorView().setSystemUiVisibility(
            View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LOW_PROFILE
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_IMMERSIVE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
}

before adding this call this below super.onCreate(savedInstanceState); onCreate

    hideSystemUI();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
        getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
    }

above attribute is use if your device have notch cutout for camera.

Swig answered 17/2, 2020 at 9:40 Comment(5)
This should be the accepted answer. Nothing else worked for me.Safeguard
This is so simple and working, Nothing else worked for me.Reata
Worked for me and this should be the accepted answer.Crusade
This is deprecated, according to the docs you should use WindowInsetsController. More info here https://mcmap.net/q/173357/-android-view-view-systemuivisibility-deprecated-what-is-the-replacement/4544940Centuple
Last line helped me to stop status bar blacking out on MIUI 13 (API 31)Silvers
C
1

In addition to your code you just need adding this code before your setContentView in your activity

this.requestWindowFeature(Window.FEATURE_NO_TITLE);
Cobaltite answered 29/5, 2019 at 5:45 Comment(0)
T
1

So thanks to this video I got the solution.

First of all app theme should extend a NoActionBar Theme

<style name="Theme.YourApp" parent="Theme.MaterialComponents.DayNight.NoActionBar"></style>

There after in the Activity onCreate Method (In this case using viewBinding) should implemented as follows.

private lateinit var binding : ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    requestWindowFeature(Window.FEATURE_NO_TITLE)

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
        window.attributes.layoutInDisplayCutoutMode = WindowManager
              .LayoutParams
              .LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
    }

    WindowCompat.setDecorFitsSystemWindows(window, false)

    binding = ActivityMainBinding.inflate(layoutInflater)

    WindowInsetsControllerCompat(window, binding.root).let { controller ->

        controller.hide(WindowInsetsCompat.Type.systemBars())
        controller.systemBarsBehavior = WindowInsetsControllerCompat
            .BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE

    }

    setContentView(binding.root)

}

This worked for me. Hope it does for you as well.

Truth answered 27/6, 2022 at 14:3 Comment(0)
Q
0

Here's a way to implement the edge-to-edge display starting from Android Q. The code is inspired by this blog.

First, in your styles.xml, modify the AppTheme like:

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
        ...
    <item name="android:navigationBarColor">@android:color/transparent</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
</style>

Suppose, we need to make the MainActivity fullscreen, then in MainActivity.kt:

window.decorView.systemUiVisibility = (
                View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        or View.SYSTEM_UI_FLAG_FULLSCREEN
                )

Also, check the docs here for enabling fullscreen mode.

Quinidine answered 29/5, 2019 at 5:47 Comment(3)
Tried, failed. Samsung S10+.Inmate
Which Android version you were using?Quinidine
Samsung S10+, Android PieInmate
A
0

First, in your styles.xml, modify the AppTheme like:

<style name="Theme.AppCompat.Light.NoActionBar.FullScreen" parent="@style/Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowActionBar">false</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>
</style>

and also mention in your manifest file.

<activity
   android:name=".activities.FullViewActivity"
   android:theme="@style/Theme.AppCompat.Light.NoActionBar.FullScreen" 
/>
Augment answered 28/6, 2022 at 11:39 Comment(0)
H
-1

Try this:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    Window window = getWindow();
    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    window.setStatusBarColor(getResources().getColor(R.color.colorPrimary));
} else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
    Window window = mContext.getWindow();
    window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    int statusBarHeight = (int) dpToPx(24);

    View view = new View(mContext);
    view.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
    view.getLayoutParams().height = statusBarHeight;
    ((ViewGroup) window.getDecorView()).addView(view);
     view.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
}

dpToPx

public float dpToPx(float dp) {
    return (dp * Resources.getSystem().getDisplayMetrics().density);
}
Hibben answered 29/5, 2019 at 5:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.