Matt Boyle
Matt Boyle

Reputation: 385

Using my Lollipop theme (on Lollipop)!

I had my app looking pretty nice using the new Lollipop tools. I decided backwards compatibility is important, so I switched all my Fragments,actionBar imports to the support library. Now (understandably) I can't use my lollipop theme.

Is there a way to use different action bars for different themes? I tried to cast the support ActionBar to a new one but it doesn't seem this is allowed.

My problem lies with the following (from v21 docs)

All of your Activities must extend from ActionBarActivity, which extends from FragmentActivity from the v4 support library, so you can continue to use fragments. All of your themes (that want an Action Bar/Toolbar) must inherit from Theme.AppCompat. There are variants available, including Light and NoActionBar. When inflating anything to be displayed on the action bar (such as a SpinnerAdapter for list navigation in the toolbar), make sure you use the action bar’s themed context, retrieved via getSupportActionBar().getThemedContext(). You must use the static methods in MenuItemCompat for any action-related calls on a MenuItem.

so by calling getsupportActionBar I can't use my Holo theme:

 <resources>
<!-- Base application theme. -->
<style name="appTheme" parent="android:Theme.Holo.Light.DarkActionBar">

</style>


<style name="MyActionBar"
    parent="android:Theme.Holo.Light.DarkActionBar">
    <item name="android:background">@color/blue_semi_transparent</item>
</style>
</resources>

Also for some reason the action bar loses the button that was on it and it goes into the dropdown menu and the app icon no longer appears in the action bar. I really am no expert on this stuff having only started developing on lollipop so would really appreciate advice.

Upvotes: 0

Views: 393

Answers (1)

ianhanniballake
ianhanniballake

Reputation: 199880

AppCompat (i.e., ActionBarActivity) uses the Material color palette which defines default coloring throughout your app. In your case, you need to use colorPrimary for your action bar color:

<style name="appTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/blue_semi_transparent</item>
</style>

Note that you should also provide a colorPrimaryDark (a darker version of the same color) for coloring the status bar.

Per the partially outdated Action Bar training, AppCompat also uses app namespaced attributes (as things like showAsAction didn't exist before API 11) for your menu items:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto" >
    <item android:id="@+id/action_search"
        android:icon="@drawable/ic_action_search"
        android:title="@string/action_search"
        app:showAsAction="ifRoom"  />
  ...
</menu>

Per the Toolbar documentation (which is default behavior on Material themes and in AppCompat):

In modern Android UIs developers should lean more on a visually distinct color scheme for toolbars than on their application icon. The use of application icon plus title as a standard layout is discouraged on API 21 devices and newer.

Therefore as you noted the app icon not appearing on the Action Bar is expected behavior.

Upvotes: 2

Related Questions