ActionBarCompat (Part 1): How to use

Google has released a new version of its Support Library and it finally includes ActionBarCompat. We don’t know if ActionBarSherlock days of glory are over, but it’s true that the Android team has been working on ActionBarCompat so hard that it deserves at least one chance.

Coding ActionBarCompat

So let’s create a new project using API 18 and add the project under sdk\extras\android\support\v7\appcompat folder. I will set light theme with dark Action Bar. It is as easy as usual:

[xml]
<application
android:label="@string/app_name"
android:icon="@drawable/ic_launcher"
android:theme="@style/Theme.AppCompat.Light.DarkActionBar">
[/xml]

Now your activity needs to extend ActionBarActivity instead. This activity is based on FragmentActivity, so you will be able to use fragments without any extra effort. It’s easy:

[java]
public class MainActivity extends ActionBarActivity {
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
[/java]

And here it is!

Action Bar Compat

But what if we want to add some menu items? It’s pretty much the same, but some attributes require our custom namespace:

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

That’s the case of showAsAction, or actionViewClass, which will be explained in next episode. Not too difficult, right?

Now you can inflate the menu:

[java]
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
[/java]

actionbarcompat2

If you need to access Action Bar programatically, you will call getSupportActionBar()

Conclusion

Using ActionBarCompat is almost as easy as native bar, and even similar to ActionBarSherlock, but there are a few differences that must be know. This first tutorial covered the most simple integration. You can find full code on Github.

In next episodes, I will explain how to include an action view, change action mode or even integrate Navigation Drawer. So please, stay tuned!

ActionBarCompat (Part 2): Action Views >

39 thoughts on “ActionBarCompat (Part 1): How to use”

  1. Have you done any work yet trying to get Custom Themes within the support action bar? We have the new Action Bar up and running but unfortunately I am having a lot of difficulty getting our custom theme to work.

    1. Antonio Leiva

      Not yet, I write this tutorials at the same time I investigate how to use it. I’ll write about it soon.

    2. Trino Espinoza

      I did it, you can use Action Bar Style Generator by Jeff Gilfelt here. It supports ActionBarCompat :D.
      Once you’ve downloaded your custom theme, place the files on the res folder and modify AndroidManifest.xml file and replace with

  2. Trino Espinoza

    Thanks. Does it supports styles from the ActionBarStyleGenerator project?

  3. Crossle Song

    I think you can add app:actionViewClass=”android.support.v7.widget.SearchView” too.

  4. Looking forward to more of this! Quick question how did you do the screenshots with the phone? I thought there was a program that did this for you but I cant remember? Thanks

  5. Im trying a code similar to yours, but im getting an error with the showAsAction attribute–> error: No resource identifier found for attribute ‘showAsAction’ in package ‘com.omarbs’

    Im looking the full code on your GitHub but i dont see any difference, or at least where are you definning the attribute.

    Thanks!

    1. Antonio Leiva

      Is the rest of the code compiling? It seems some library is not well included. Probably android-support-v7-appcompat.jar. It’s included in appcompat project, but you may need include in your main project depending on your IDE.

  6. I am trying to write a new application that follows Google’s standards, and this does seem to be helpful.

    However, I am wondering about the dataset you are using to get all of this group / artist information. I am looking for one that has a good amount of Korean groups and artists in particular, but don’t really know what to look for, or how to go about looking for one.

    Another question…ActionBarCompat is to allow older versions of Android the ability to use Action Bars, correct. They can we just implement ActionBarCompat if we are trying to target older versions of Android, or do we have to also use the native way of implementing Action Bars?

    I might be able to test the last question out if I start this tutorial today, but thought I would still ask.

    1. Antonio Leiva

      Hi Christopher. I use Deezer as my main data source. Don’t know if it serves enough music of that kind. There are many more, such as Last.FM or Echonest.

      ActionBarCompat is only useful if you are creating Apps below API 11. However, if you use it, you don’t need to make a difference between new and old versions. Simply use ActionBarCompat for everything. The library will choose by itself using the native one or a “fake” one depending on the device version.

  7. OK, so I am running into a problem from the very start!

    In Eclipse, I create a new Android project with tartget API 18 (Kept all other default values). Went to the build path and added the external library android-support-v7-appcompat.jar from ..\sdk\extras\android\support\v7\appcompat\libs.

    From there I ONLY changed extends Activity to extends ActionBarActivity, and import android.support.v7.app.ActionBarActivity…along with made the manifest change from Holo to AppCompat

    I start up the emulator and run the project >> ERROR >> “Unfortunately, AppBarCompat has stopped.”

    Change extends ActionBarActivity back to extends Activity, and run project >> the project runs fine.

    I looked at the DevBytes YouTube video for using ActionBarCompat, and implemented what they have at 1:40 (http://youtu.be/6TGgYqfJnyc)

    Just moving what you stated in the Application tag, and move it to the activity tag. I still get an error mark on the android:theme line stating “error: Error: No resource found that matches the given name (at ‘theme’ with value ‘@style/
    Theme.AppCompat.Light.DarkActionBar’).”

    1. Antonio Leiva

      Did you add AppCompat project to the main project as a library? It seems there’s something left. You may also need to add android-support-v4.jar to libs folder. Both are in AppCompat project. Please test both things.

      1. Under Referenced Libraries
        – android-support-v7-appcompat.jar

        Under libs
        – android-support-v4.jar

        I am not sure if I added AppCompat project to my main project as a library.

        If it is going to Project Properties >> Android >> Library, then there are no libraries there, and clicking Add… does not allow me to do anything. Like this here (http://wptrafficanalyzer.in/blog/implementing-action-bar-using-actionbarcompat-support-library-in-android/)

        So then I assume I am doing “Setup ActionBarCompat library in Eclipse” wrong. I will try this again, but I did not see this indicated anywhere in your post, and thought this was way overboard…I will try it again.

      2. Antonio Leiva

        Sorry, I tried to do it as cross-IDE as possible, so I didnt’ get into details. I use IntelliJ. You need to choose new -> Add project from existing source, and select AppCompat folder. Add it, and then you should be able to add it as a library on that place. I haven’t tried myself on Eclipse, but if you don’t get it working, tell me and I try.

      3. OK…I had to follow the following to the T

        http://developer.android.com/tools/support-library/setup.html#libs-with-res

        I have been able to add that project as a library…finally. However, I am not able to build my project (I even created another new project). It might be because Eclipse is not letting me add (just) the existing Android project…so I had to add an existing (generic) project. That might be why I am running into some problems.

        I can add the other two projects in v7 no problem, but appcompat is not allowing me to.

        So I am downloading the updates to the SDK Manager in Android Studio now, and going to add the line to build.gradle as by the link above, and see if I have any luck with that.

  8. Ahmad Ali Nasir

    Everything works fine except for one thing, the menu overflow icon isn’t displayed. The settings option is displayed when clicked on the device settings button. I compiled your project as well, and it has the same issue.

    1. Hi Rafael, Did you ever find a fix for this issue? I seem to be running into the same issue using ActionbarCompat on Gingerbread.

  9. Hi Antonio Do you know how to add dropdown(Spinner) in the actionbar v7 library and that should show fragments breadcrumbs as items in the dropdown.

  10. I need help.. The ActionBar with the icons work well but how do i make it so that it becomes an overflow menu button such that of the normal Android 4.0 I did tried using app:showAsAction=”never” But it seems it can’t work, it will direct the button back to the normal android 2.3 where users have to press the menu button. Thank you

    1. As far as I know, that’s the standard behaviour in every Action Bar library. If the device has a menu button, overflow menu icon won’t appear on Action Bar, and the options will appear when the user presses the menu button. It’s been a long time since I used regular ActionBar, but ActionBarSherlock and ActionBarCompat work that way, so I can imagine the standard one will do the same.

      1. I get the same: error: No resource identifier found for attribute ‘showAsAction’ in package
        I have eclipse, made a new project included both jars in buildpath, still same error.

  11. Antonio Leiva

    George, did you add this to the menu xml declaration?

    xmlns:app=”http://schemas.android.com/apk/res-auto”

    If not, showAsAction can’t be found, because iti’s not using the standard android tag.

  12. selim furkan seyhan

    It didn’t work for me when I tried to add a custom actionViewClass to my MenuItem with following line

    app:actionViewClass=”MyCustomSearchView”

    MyCustomSearchView extends android.support.v7.widget.SearchView

    MenuItemCompat.getActionView(searchItem); returns null.

    Isn’t this supported?

Comments are closed.