Search code examples
androidandroid-layoutandroid-5.0-lollipopandroid-actionbar-compat

appcompat v7 23, actionbars, transparency


I think I went all over SO for the answer. I think I am being trolled by Android L... Seems no matter what I do that actionbar's background still defaults to colorPrimary

the layout.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"          android:id="@+id/dl_main"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CoordinatorLayout
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.farmdog.farmdog.MainActivity">

        <android.support.v4.view.ViewPager
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_anchor="@+id/appbar"
            app:layout_anchorGravity="top"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
        <!--layout_behavior="TransparentScrollingViewBehavior"/>-->

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <!--android:theme="@style/AppTheme.AppBarOverlay">-->

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingTop="8dp"
                android:paddingBottom="8dp"
                app:layout_scrollFlags="scroll|enterAlways"/>
            <!--android:background="?attr/colorPrimaryDark"-->
                <!--app:theme="@style/AppTheme.AppBarOverlay">-->
                <!--android:layout_height="?attr/actionBarSize"-->
                <!--app:popupTheme="@style/AppTheme.PopupOverlay"-->
        </android.support.design.widget.AppBarLayout>
........

and styles.xml (v21)

 <resources>
        <!-- Base application theme. -->
        <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
            <!-- Customize your theme here. -->
            <item name="colorPrimary">@color/colorPrimary</item>
            <!-- colorPrimaryDark wont work on API level < 21 -->
            <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
            <item name="colorAccent">@color/colorAccent</item>
            <item name="android:windowBackground">@color/mainBkgndHard</item>
            <item name="android:textColor">@color/appTextColor</item>
            <item name="android:buttonStyle">@style/ButtonTheme</item>
            <!--<item name="android:fitsSystemWindows">true</item>          hmmm, primaryColor expands on the statusbar now...-->
            <!-- attempt taking actionbar shadow away -->
            <item name="elevation">0dp</item>
            <item name="android:windowContentOverlay">@null</item>
            <!-- the following line breaks the build-->
            <!--<item name="windowContentOverlay">@null</item>-->
            <!-- attempt drawing underneath the actionbar - all fail as of 'com.android.support:appcompat-v7:23.1.1.'-->
            <item name="windowActionBarOverlay">true</item>
            <item name="actionBarStyle">@style/MyActionBar</item>
            <item name="android:windowActionBarOverlay">true</item>
            <item name="android:actionBarStyle">@style/MyActionBar</item>
        </style>

        <!--<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar">-->
            <!--<item name="android:background">@color/colorActionBar</item>-->
            <!--<item name="background">@color/colorActionBar</item>-->
            <!--<item name="windowActionBarOverlay">true</item>-->
            <!--<item name="android:windowActionBarOverlay">true</item>-->
        <!--</style>-->



        <style name="MyActionBar" parent="Widget.AppCompat.Light.ActionBar.Solid">
            <item name="android:background">@color/colorActionBar</item>
            <!--For compatibility-->
            <item name="background">@color/colorActionBar</item>
        </style>

        <style name="AppTheme.NoActionBar">
            <item name="windowActionBar">false</item>
            <item name="windowNoTitle">true</item>
            <item name="android:windowDrawsSystemBarBackgrounds">true</item>
            <item name="android:statusBarColor">@android:color/transparent</item>
        </style>


    </resources>

Solution

  • I think you are doing it in the wrong way.I mean, the following Toolbar Hasn't any PrimaryColor or even Background:

    <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingTop="8dp"
                    android:paddingBottom="8dp"
                    app:layout_scrollFlags="scroll|enterAlways"/>
    
                <!--android:background="?attr/colorPrimaryDark"-->
                    <!--app:theme="@style/AppTheme.AppBarOverlay">-->
                    <!--android:layout_height="?attr/actionBarSize"-->
                    <!--app:popupTheme="@style/AppTheme.PopupOverlay"-->
    

    So, this will works on (v21) versions only without any background or PrimaryColor.and as you can see, Toolbar is going to be like your Activity Theme which that has:

     <item name="colorPrimary">@color/colorPrimary</item>
    

    With:

    Theme.AppCompat.Light.DarkActionBar
    

    And, if you don't set any background color or PrimaryColor, because of:

     Theme.AppCompat.Light.DarkActionBar
    

    and your Toolbar, it has to be like that:

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
                <!-- Customize your theme here. -->
                <item name="colorPrimary">@color/colorPrimary</item>
    

    Otherwise, it should crashed(i guess, not sure) because you have a Theme with DarkActionBar and one Toolbar with no background.

    I think this is the problem!

    Edit:

    For completing the answer, since this code was simple anyway, i'll add it here:

    Add this also in your AppBarLayout:

    android:theme