Can I set FLAG_LAYOUT_NO_LIMITS only for status bar?

25,587

Solution 1

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!

Solution 2

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

Solution 3

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

Solution 4

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

Solution 5

fun showTransparentStatusbar() {
        activity!!.window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN)
    }


    fun removeStatusbarFlags() {
        activity!!.window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
    }
Share:
25,587
BArtWell
Author by

BArtWell

Updated on August 27, 2020

Comments

  • BArtWell
    BArtWell almost 4 years

    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?

  • samurdhilbk
    samurdhilbk over 6 years
    This solution does the job as described. The navigation bar is opaque and there is no content under it.
  • Robokishan
    Robokishan over 5 years
    which Android are you using because its for older android 6 or 7
  • Shashank Degloorkar
    Shashank Degloorkar over 5 years
    Perfect and concise answer. Kudos
  • BST Kaal
    BST Kaal over 5 years
    Finally ! A precise and original answer.
  • Aanal Shah
    Aanal Shah about 5 years
    It worked for me. Additionally all I have to do is just adjust my layout top according to status bar. Thank you so much :)
  • Rajkiran
    Rajkiran almost 5 years
    @NikunjPatel It worked for me (and 4 others). Can you post your code as a question where it is not working for you and send the link here. Maybe I can help.
  • smoothumut
    smoothumut almost 5 years
    thanks only this one worked for me. clean and precise. no need for extra info
  • Abdul Mateen
    Abdul Mateen over 4 years
    It doesn't make the status bar transparent. It simply hides the status bar.
  • phamxuanlu
    phamxuanlu over 4 years
    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.
  • Karsh Soni
    Karsh Soni almost 4 years
    prevent scroll view from scrolling
  • Alexey Timokhin
    Alexey Timokhin almost 4 years
    That doesn't work on Samsung SM-T295 (Android 10, One UI 2.1). The navigation bar appears on top of the content.
  • Alexey Timokhin
    Alexey Timokhin almost 4 years
    it makes the status bar disappear
  • AndreasReiff
    AndreasReiff over 3 years
    This does not work! I can't set light theme on the navigation bar. Only white buttons with black translucent background.
  • Shunan
    Shunan about 3 years
    You are right.. i tried it and didn't regret it
  • hmac
    hmac over 2 years
    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)
  • famfamfam
    famfamfam over 2 years
    FLAG_LAYOUT_NO_LIMITS hide actionbar
  • famfamfam
    famfamfam over 2 years
    did u solved this?
  • famfamfam
    famfamfam over 2 years
    this hide my toolbar?
  • famfamfam
    famfamfam over 2 years
    this hide my toolbar?
  • famfamfam
    famfamfam over 2 years
    you just adjust margin of your view, so what did i do if i have toolbar?
  • Eugene P.
    Eugene P. over 2 years
    That was 3 years ago ) Yep. WindowManager.LayoutParams.FLAG_FULLSCREEN worked for nexus 5.
  • TheJudge
    TheJudge over 2 years
    what do you mean ? I set margin to root component of my layout. In that layout I also have Toolbar