1

I have to make layout, where top of the layout should be drawn under SystemBars. (min API level for this app is 22)

I set these flags to achieve that. In other activities I can draw whole fragment with views under SystemBars, but here I cant do it.

I used this code to allow layout to be drawn under SystemBars

window.apply {
        addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
        statusBarColor = resources.getColor(android.R.color.transparent)
        navigationBarColor = resources.getColor(android.R.color.transparent)
        setBackgroundDrawable(background)
        decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
    }

This is result in app:

enter image description here

I need to draw that grey area under system bar.

Update:

@Redman solution worked only partially. Now I can draw under StatusBar but also I can draw under software buttons (navigationButtons) as you can see at screen below. Is there any way how to allow layout to be drawn under SystemBars but do not allow it under navigationButtons? Because that code what I've posted works for different fragments in app. But in other fragments I don't have ImageView as a part of ToolBar. If you have just ToolBar with for example 2-color gradient background and some backButton + SearchView, it is working fine. But as I add ImageView, ImageView will not "slide" under top SystemBar(aka StatusBar). Even in image above you can clearly see that green color (it's ToolBar background drawn under StatusBar). But ImageView is not allowed to be drawn under that bar.

The only way how to achieve that was with that special line of code

window.apply { 
        setFlags( WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)

    }

but this line also allowed to draw layout content under NavigationButtons (which is an issue).

enter image description here

martin1337
  • 2,384
  • 6
  • 38
  • 85

4 Answers4

1

In your Activity Theme add these properties

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

and in parent layout add property

 android:fitsSystemWindows="true"

Edit

Try this to do it programmatically, it should work above on devices having kitkat and above

    window.apply { 
        setFlags( WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
    }
Manohar
  • 22,116
  • 9
  • 108
  • 144
  • true true I cant add this to style. It will make my system Bar darker. I need it fully transparent. What I will add to parent layout background, it should be background of systembar aswell. And fitsSystemWindows="true" will make whole layout to fit bottom system bar buttons (if you have software buttons). And those buttons will be covered by my layout. – martin1337 Sep 07 '18 at 08:57
  • This is working properly for status bar but navigation bar is broken. If you have software buttons at the bottom of the screen, my whole layout is drawn over those buttons. I want to keep buttons intact and layout should be drawn over status bar only. – martin1337 Sep 13 '18 at 08:15
1

use toolbar view in your xml file. See below example

    <android.support.v7.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="@dimen/height_50dp"
            android:background="#f1f1f1">

\\define your any element here. Like back arrow or title

</android.support.v7.widget.Toolbar>
Praveen Kumar
  • 277
  • 2
  • 12
  • whole header is located inside toolbar layout. Inside toolbar layout is CoordinatorLayout with imageViews and TextViews + buttons. – martin1337 Sep 13 '18 at 09:52
0

All you need to do is set these properties in your theme:

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

Your activity / container layout you wish to have a transparent status bar needs this property set:

android:fitsSystemWindows="true"

It is generally not possible to perform this for sure on pre-kitkat, looks like you can do it but some strange code makes it so.

EDIT: I would recommend this lib: https://github.com/jgilfelt/SystemBarTint for lots of pre-lollipop status bar color control.

Well after much deliberation I've learned that the answer to totally disabling the translucency or any color placed on the status bar and navigation bar for lollipop is to set this flag on the window:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window w = getWindow(); // in Activity's onCreate() for instance
            w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        }
ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108
Ashutosh
  • 109
  • 6
0

How about this:

Window window = getWindow();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(ContextCompat.getColor(this, R.color.primary_dark));
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        int flags = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        window.addFlags(flags);
    }

This should make Only StatusBar transparent.


If this didn't help, try adding:

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

To the Activity styles code in styles.xml.

ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108