ActionBar

ActionBarCompat – Part 2

In the previous article we began converting the existing project from the Styling Android series on ActionBar to use the ActionBarCompat library. Once we had got a Gradle build working, we found that we had a lot of resource errors once we switched the compiler to compile against API 7. In this article we’ll look at resolving those errors.

The primary cause of our errors is that the styles that we use in res/styles.xml are dependent on the Holo style and attributes which were added in later versions of Android.

There are some new themes which have been included in the ActionBarCompat library named Theme.AppCompat, Theme.AppCompat.Light, and Theme.AppCompat.Light.DarkActionBar. These are roughly equivalent to their counterparts in API 11+ Theme.Holo, Theme.Holo.Light, and Theme.Holo.Light.DarkActionBar.

So we need to change all of our themes and styles which are reliant on Holo ones to use their AppCompat equivalents.



    
    .
    .
    .

Once again, I have not included the entire file here.

The next thing that we need to do is change our menu resources. In OS versions which include ActionBar support additional attributes were added to the menu resources to control how menus are displayed within the ActionBar:



    
    
    
    
    
    
    


Obviously these attributes are not supported on 2.x, so we have to ensure that we use the ones defined within the appcompat library. We do this by using the namespace of the local app resources, and specifying these particular attributes using that namespace prefix instead:



    
    
    
    
    
    
    


Fortunately there is no need to duplicate these resources as with the style items.

We need to do this for our actionmode.xml menu as well:



    
    
    
    


That's the resources completed, although we'll need to tweak things a little once we get the app running and have some minor issues to resolve.

In the next article we'll turn our attention to our Java code, and make the necessary changes to that.

The source code for this article is available here.

© 2013 - 2014, Mark Allison. All rights reserved.

Copyright © 2013 Styling Android. All Rights Reserved.
Information about how to reuse or republish this work may be available at http://blog.stylingandroid.com/license-information.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.