Philip Kahn
Philip Kahn

Reputation: 713

AppCompatActivity not using colorPrimary for Action Bar?

According to https://developer.android.com/training/material/theme.html , colorPrimary should set the action bar color.

This works when my mainActivity extends Activity, but it breaks when I extend instead AppCompatActivity (or the now-deprecated ActionBarActivity).

So for AppCompatActivity I must use:

values/style.xml

<style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimaryDark">@color/MaterialTeal800</item>
    <item name="colorPrimary">@color/MaterialTeal500</item>
    <!--<item name="colorPrimaryLight">@color/MaterialLightPrimary</item>-->
    <!--<item name="colorDivider">@color/MaterialDivider</item>-->
    <item name="colorAccent">@color/MaterialDeepOrange500</item>
    <!--<item name="colorAccentPressed">@color/MaterialDeepOrange800</item>-->
</style>
<style name="AppTheme" parent="AppBaseTheme">
    <!-- Customize your theme here. -->
    <item name="android:textColor">@color/black</item>
    <item name="android:textColorSecondary">@color/MaterialSecondary</item>
    <item name="android:textColorPrimary">@color/black</item>
    <item name="actionBarTabTextStyle">@style/TitleText</item>
</style>
<style name="TitleText" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:textColor">@color/white</item>
</style>

which colors the status bar correctly, but NOT the action bar.

And (for v21, anyway) for Activity it works with

 <style name="AppBaseTheme" parent="android:Theme.Material.Light.DarkActionBar">
    <!-- android:Theme.Material.Light.DarkActionBar -->
    <!-- Theme.AppCompat.Light.DarkActionBar -->
    <item name="android:colorPrimaryDark">@color/MaterialTeal800</item>
    <item name="android:colorPrimary">@color/MaterialTeal500</item>
    <!--<item name="android:colorPrimaryLight">@color/MaterialLightPrimary</item>-->
    <item name="android:textColor">@color/black</item>
    <item name="android:actionBarTabTextStyle">@style/TitleText</item>
    <item name="android:textColorSecondary">@color/MaterialSecondary</item>
    <!--<item name="android:colorDivider">@color/MaterialDivider</item>-->
    <item name="android:textColorPrimary">@color/black</item>
    <item name="android:colorAccent">@color/MaterialDeepOrange500</item>
</style>

If I don't use Activity and use AppCompatActivity instead, it crashes on launch, saying:

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

I'd like to run with AppCompatActivity for, well, compatibility, but I can't figure out why it's not picking up the color (yet the same code works great with Activity -- and I'd love to use all the newer options on API 21+). It seems wrong that it'd pick up the status bar but I'd need to manually override a custom style on the action bar (a-la this answer). Am I stuck picking and choosing? No Material themes or transitions, or backward compatibility?

Note this is not a toolbar. This is the action bar -- I know I can use android:background on an added toolbar element.

Update

Looks like I can get the actionbar to color with AppCompatActivity by dropping the v21 android: namespace, but still get the crash if I try to use the real material theme ...

Upvotes: 1

Views: 2966

Answers (1)

Harin
Harin

Reputation: 2423

Here you should remove android: prefix for app compat theme like:

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

and in styles-v21

<item name="android:colorPrimaryDark">@color/MaterialTeal800</item>

Upvotes: 2

Related Questions