Correct way to style Android Lollipop 5.0 toolbar
I've been banging my head about this for a few seconds now with no luck. I am using android.support.v7.widget.Toolbar
in my app (which supports Android API 11 and above) and want to style it. This is how I do it:
My Activity Layout XML contains:
<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:theme="?attr/foo" />
res / values ββ/attr.xml (this is where I define my attributes):
<attr name="foo" format="reference" />
Res / value / Themes.xml :
<style name="AppTheme" parent="Theme_one">
</style>
<style name="Theme_one" parent="@style/Theme.AppCompat.Light">
<item name="windowActionBar">false</item>
<item name="foo">@style/foo_style</item>
<!-- bunch of other styles -->
</style>
Res / value / styles.xml :
<style name="foo_style" parent="@style/Theme.AppCompat.Light">
<item name="android:background">@color/actionBarDark</item>
</style>
Res / value-v21 / Themes.xml :
<style name="AppTheme" parent="Theme_one">
<!-- some v21 specific items not related to toolbar like animations etc-->
</style>
Res / value-v21 / styles.xml :
<style name="foo_style" parent="@style/Theme.AppCompat.Light">
<item name="android:colorPrimaryDark">@color/actionBarDark</item> <!-- THIS DOES NOT WORK !!! -->
<item name="android:background">@color/actionBarDark</item>
<item name="android:elevation">5dp</item>
</style>
This scheme works great for Android API v11 - v19. My problems for v21 (I also tried 2 and 3 without prefix android:
with the same result):
1) android: background works great!
2) android: colorPrimaryDark doesn't work!
3) android: height refers to the name of the buttons Toolbar
and Toolbar
as shown below. Is this expected?
What am I missing? It is clear that I am not doing the styling correctly, but cannot find any resources that talk about styling Toolbar
!
source to share
I got colorPrimaryDark to work by moving:
<item name="colorPrimaryDark">@color/actionBarDark</item>
from res / values-v21 / Styles.xml to res / values-v21 / Themes.xml .
I got the elevation by removing it from all styles or xmls themes and placing it in the ad android.support.v7.widget.Toolbar
.
Also, if I define a different theme here and change it dynamically in the app using Content.setTheme (), the theme changes, but the status bar color doesn't work. Any suggestions are greatly appreciated.
source to share