3

I'm trying to set a transparent status bar (like on a preview), but it looks like on video: https://yadi.sk/i/HcYJa3VB3WYGvL

Preview:

enter image description here

I tried set in styles xml's

<item name="android:windowBackground">@android:color/transparent< /item>

in different Layouts, also set

fitsSystemWindows = "true"

MainActivity.xml:

<android.support.v4.widget.DrawerLayout
    ...
    android:fitsSystemWindows="true">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coordinator_container"
        android:fitsSystemWindows="true"
        android:layout_height="match_parent"
        android:layout_width="match_parent">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/app_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay"
            android:background="@color/color_primary">

            <android.support.design.widget.CollapsingToolbarLayout
                ...
                app:contentScrim="@color/color_primary"
                android:fitsSystemWindows="true"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">

                <ImageView
                    android:id="@+id/user_photo"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/profile_image_size"
                    android:scaleType="centerCrop"
                    android:src="@drawable/user_photo"
                    app:layout_collapseMode="parallax"
                    android:fitsSystemWindows="true"/>

                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:theme="@style/AppTheme.PopupOverlay"
                    app:titleTextColor="@color/white"
                    app:layout_collapseMode="pin"/>
            </android.support.design.widget.CollapsingToolbarLayout>
        </android.support.design.widget.AppBarLayout>

        <android.support.v4.widget.NestedScrollView
            android:layout_height="match_parent"
            android:layout_width="wrap_content"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            ...

        </android.support.v4.widget.NestedScrollView>

        <android.support.design.widget.FloatingActionButton
            .../>
    </android.support.design.widget.CoordinatorLayout>

    <android.support.design.widget.NavigationView
        ...>
    </android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>

v21/style.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/color_primary</item>
        <item name="colorPrimaryDark">@color/color_primary_dark</item>
        <item name="colorAccent">@color/color_accent</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@color/color_accent</item>
        <item name="android:windowTranslucentStatus">true</item>
    </style>
</resources>   
Zoe
  • 27,060
  • 21
  • 118
  • 148
Ray Smith
  • 73
  • 1
  • 2
  • 6
  • Possible duplicate of [Android Completely transparent Status Bar?](https://stackoverflow.com/questions/29311078/android-completely-transparent-status-bar) – Faysal Ahmed May 26 '18 at 08:31
  • https://medium.com/androiddevelopers/translucent-systembars-the-right-way-across-api-levels-and-themes-6d7ddda21396 – Zero Jul 03 '21 at 10:18

4 Answers4

2

There are two different cases:

1.If you want slightly transparent status bar(as you described in your question)

<style name="ThemeActionBar" parent="Widget.AppCompat.Light.ActionBar.Solid">
  <item name="android:background"> @null </item>
  <!-- Support library compatibility -->
  <item name="background">@null</item>
  <item name="android:displayOptions"> showHome | useLogo</item>
  <item name="displayOptions">showHome|useLogo</item>

2.For Completely transparent status bar:

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

check this answer you understand how things works.

Nitin
  • 1,280
  • 1
  • 13
  • 17
1

add this in your theme

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

Set android:fitsSystemWindows=”true” to root container

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    Window w = getWindow();
    w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}

add in your Activity

Bhupat Bheda
  • 1,968
  • 1
  • 8
  • 13
1

you can use this fun :

private fun changeScreenSystemUiController(isFullScreen: Boolean) {
    window?.also {
        WindowCompat.setDecorFitsSystemWindows(it, !isFullScreen)
        WindowCompat.getInsetsController(it, it.decorView).apply {
            systemBarsBehavior =
                if (isFullScreen)
                    WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
                else
                    WindowInsetsControllerCompat.BEHAVIOR_SHOW_BARS_BY_TOUCH
            if (isFullScreen)
                hide(WindowInsetsCompat.Type.systemBars())
            else
                show(WindowInsetsCompat.Type.systemBars())
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            it.attributes.layoutInDisplayCutoutMode =
                if (isFullScreen)
                    WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
                else
                    WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT
        }
    }
}
Mahdi Zareei
  • 1,299
  • 11
  • 18
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"

Already have answer od this question. Follow: Android Completely transparent Status Bar?

Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50