Can I set FLAG_LAYOUT_NO_LIMITS only for status bar?
Asked Answered
E

13

70

I need to make transparent status bar. I am using getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS) and it is make status bar as I want. But it also affect navigation bar: it became transparent and getWindow().setNavigationBarColor(Color.BLACK) do nothing.

Is there way to make transparent status bar only and not navigation bar?

Excerpt answered 26/10, 2015 at 13:43 Comment(1)
Can you please post image of your current and expected behavior?Larry
S
38

this work for me

getWindow().setFlags(
    WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
    WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
)

styles.xml

<style name="TranslucentStatusBar" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowTranslucentStatus">true</item>
</style>

v21\styles.xml

<style name="TranslucentStatusBar" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowDrawsSystemBarBackgrounds">false</item>
    <item name="android:windowTranslucentStatus">true</item>
</style>

status bar will be transparent or translucent, navigation bar won't

hope this helps!

Swatch answered 30/8, 2018 at 8:58 Comment(3)
prevent scroll view from scrollingJuniorjuniority
That doesn't work on Samsung SM-T295 (Android 10, One UI 2.1). The navigation bar appears on top of the content.Broadbrim
This does not work! I can't set light theme on the navigation bar. Only white buttons with black translucent background.Cons
F
21

using mikepenz's comment

what I exactly working code (converted to kotlin) below here.

// at AppCompatActivity, min SDK is 16, I tested api 25
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    if (Build.VERSION.SDK_INT >= 19 && Build.VERSION.SDK_INT < 21) {
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
    }
    if (Build.VERSION.SDK_INT >= 19) {
        window.decorView.systemUiVisibility =
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
    }
    if (Build.VERSION.SDK_INT >= 21) {
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        window.statusBarColor = Color.TRANSPARENT
    }

    setContentView(R.layout.activity_main)
}
Finback answered 11/1, 2018 at 4:48 Comment(1)
window.decorView.systemUiVisibility = window.decorView.systemUiVisibility or View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN Is better (I needed this to keep status bar icons black instead of white)Landward
P
16

Scroll down to check how the end result looks like

First of all, define your styles.xml something like this-

styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
</style>

DO NOT add the following line

<item name="android:windowTranslucentStatus">true</item>

Adding above line will NOT shift the layout up when the soft keyboard is shown on a Dialog with an EditText

Then override this style in v21 and v23 styles like this-

v21/styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
     <item name="android:windowDrawsSystemBarBackgrounds">false</item>
</style>

v23/styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
     <item name="android:windowDrawsSystemBarBackgrounds">false</item>
</style>

Activity code - Kotlin

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    window.setFlags(
            LayoutParams.FLAG_LAYOUT_NO_LIMITS,
            LayoutParams.FLAG_LAYOUT_NO_LIMITS
    )
    setContentView(R.layout.YOUR_LAYOUT_RESOURCE_ID)
    .
    .
.
}

Activity code - Java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(
            LayoutParams.FLAG_LAYOUT_NO_LIMITS,
            LayoutParams.FLAG_LAYOUT_NO_LIMITS
    )
    setContentView(R.layout.YOUR_LAYOUT_RESOURCE_ID)
    .
    .
    .
}

End result End result

Parthenia answered 3/3, 2019 at 18:58 Comment(2)
Almost device is work well but It isn't work on Samsung Note 10+ running Android 10 and One UI 2.0 The navigation bar still transparent and overlap app's content.Blamable
FLAG_LAYOUT_NO_LIMITS hide actionbarAnacardiaceous
L
11
getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
Lorimer answered 28/11, 2016 at 9:6 Comment(0)
S
5
fun showTransparentStatusbar() {
        activity!!.window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN)
    }


    fun removeStatusbarFlags() {
        activity!!.window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
    }
Solipsism answered 30/7, 2018 at 5:0 Comment(1)
it makes the status bar disappearBroadbrim
T
4

Try this and wont regret it

window.decorView.systemUiVisibility = (
     View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
     View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
)
Thyroiditis answered 26/8, 2020 at 20:6 Comment(0)
H
3

You can use like this to hide status bar and navigation bar

WindowManager.LayoutParams attributes = getWindow().getAttributes();
    attributes.flags |= WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
    getWindow().setAttributes(attributes);

and for showing the navigationBar again use this

   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

    }

the color is grey for me, maybe you can force it to your primary color

Hornback answered 24/2, 2016 at 18:44 Comment(0)
N
3
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

Just flag above worked for me. Navigation buttons are visible, status bar and action bar are hidden.

getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

Is not working. Test device nexus 5.

Nadene answered 23/3, 2018 at 12:35 Comment(1)
That was 3 years ago ) Yep. WindowManager.LayoutParams.FLAG_FULLSCREEN worked for nexus 5.Nadene
T
1

i found the solution bro

<style name="transparent" parent="Theme.AppCompat.Light.NoActionBar">//AppCompat is the key; You can choose any other theme than the light-theme, but stick to AppCompat
    <item name="android:windowTranslucentStatus">false</item>
    <item name="android:windowDrawsSystemBarBackgrounds">false</item>
    //Other styling(optional)
</style>

and then apply this transparent theme to your activity manifest like this

 <activity
      ...
      android:theme="@style/transparent"/>
Tartan answered 24/7, 2017 at 8:3 Comment(1)
which Android are you using because its for older android 6 or 7Tartan
T
1

For all those interested in workarounds/hacks because Android API concerning this matter is just awful. Do not use any system window flags, set negative margin to the root of your layout, and then use setStatusBarColor to make your StatusBar transparent.

statusBarHeight = getResources().getDimensionPixelSize(R.dimen.margin_24dp);
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId != 0) {
    statusBarHeight = getResources().getDimensionPixelSize(resourceId);
}

FrameLayout.LayoutParams rootParams = (FrameLayout.LayoutParams) binding.getRoot().getLayoutParams();
rootParams.topMargin = -statusBarHeight;
binding.getRoot().setLayoutParams(rootParams);

getWindow().setStatusBarColor(getResources().getColor(R.color.transparent));

where binding.getRoot() is of course the root of your layout

and result is

enter image description here

Trimetrogon answered 3/7, 2020 at 14:35 Comment(0)
K
0

yes , You can use this code in Style.xml

<style name="transparent" parent="Theme.AppCompat.Light">//AppCompat is the key; You can choose any other theme than the light-theme, but stick to AppCompat

    <item name="colorPrimaryDark">#00000000</item>//THIS is the important part.
    //Other styling(optional)
</style>

Then to apply it to your layout, simply add the following line in the root layout(view):

android:theme="@style/transparent"
Kerrykersey answered 15/6, 2017 at 6:40 Comment(0)
B
0
getWindow().setFlags(
    WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
    WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
)

<style name="TranslucentStatusBar" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowTranslucentStatus">true</item>
</style>
Blues answered 8/4, 2019 at 5:58 Comment(0)
S
-1

This works on Samsung S10+ (Pie), Oppo F7 and Oppo F5S (Oreos):

getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_OVERSCAN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

As asked by the original poster, the app area is expanded on top and covers the status bar (where the clock is). But the android navigation (virtual) buttons remains at the bottom of the screen and the app sits on top of the android buttons.

Sadiron answered 17/10, 2019 at 13:15 Comment(1)
It doesn't make the status bar transparent. It simply hides the status bar.Gascon

© 2022 - 2024 — McMap. All rights reserved.