Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Status bar not transparent when using new support design library

I'm facing some issues when trying to set a transparent status bar in my app while using the new android support design library.

Specifically, I'm using the combination CoordinatorLayout + AppBarLayout + CollapsingToolbarLayout + Toolbar with this hierarchical order.

I noticed that the status bar first come transparent then I can see the translation from transparent to full black.

I do this in values/styles:

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

And this in my layout:

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:elevation="4dp"
    android:background="?android:colorPrimary"
    android:layout_width="match_parent"
    android:layout_height="128dp">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsingToolbarLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:expandedTitleMarginStart="64dp">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:minHeight="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    </android.support.design.widget.CollapsingToolbarLayout>

</android.support.design.widget.AppBarLayout>

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginEnd="16dp"
            app:cardCornerRadius="4dp"
            app:cardElevation="6dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Latest Stories"/>

                <TextView
                    android:id="@+id/latest_story_title"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Title Example"/>

                <TextView
                    android:id="@+id/latest_story_preview"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Content Example, this should be around 250 chars"/>

            </LinearLayout>

        </android.support.v7.widget.CardView>

    </LinearLayout>

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

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab_add_story"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:fabSize="normal"
    app:layout_anchor="@id/appbar"
    app:layout_anchorGravity="bottom|right|end"
    app:borderWidth="0dp"
    android:layout_margin="16dp"/>

<View
    android:id="@+id/ripple_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="visible"
    android:elevation="10dp"
    android:stateListAnimator="@null"
    android:clickable="false"/>

(Sorry for terrible formatting, it seems like SO editor won't collaborate)

Will this be fixed in a future release or there is a way to bypass this?

Thanks everyone.

like image 903
Matteo Avatar asked Jul 06 '15 15:07

Matteo


People also ask

What is a status bar in Android?

The status bar on Android is the bar of icons running across the top of your screen. The top right corner is dedicated to the major status of your device, while the left is used for app notifications.


2 Answers

Try to set translucent flag - it was added in api level 19:

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

http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_TRANSLUCENT_STATUS

Edit: It's not working in api level lower than 19. (Minimum KitKat required)

like image 123
krystian71115 Avatar answered Oct 14 '22 14:10

krystian71115


Add this lines to values/styles.xml

 <style name="MyTheme" parent="Base.MyTheme"></style>


     <style name="Base.MyTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
   <item name="colorPrimary">@color/primary_500</item>

   <item name="colorPrimaryDark">@color/primary_700</item>

   <item name="colorAccent">@color/accent_500</item>
   <item name="windowActionBar">false</item>
    <item name="windowActionBarOverlay">true</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowBackground">@color/background_material_light</item>

    

Add below code to values-v21/styles.xml

<style name="MyTheme" parent="Base.MyTheme">
  <item name="android:windowDrawsSystemBarBackgrounds">true</item>
 </style>

Note : You can only set translucent status with this method on Lollipop and above APIs

Set application level theme to MyTheme

like image 42
Harsh Avatar answered Oct 14 '22 13:10

Harsh