Setting up the new ActionBarCompat in Android Studio
Continuing from my previous post, I’m toying around with a couple new projects, and I wanted to use some of the new compatibility libraries that Google’s offering for Android. Unfortunately, the setup is a bit involved, and the instructions are a bit scattered. I figured that it might be worth collecting them in a quick post. My main source of this info was the following DevBytes video:
Create a new project in Android Studio. I’ll skip the long explanation here, but if you have trouble with it, check out my previous post, it should get you going.
EDIT: Don’t forget to install the Support Libraries in the Android SDK manager.
The Gradle dependencies that we need are the same as was shown in my previous post, but here’s the
build.gradle file for this project, just the same:
Add dependency in
Adding a theme to the Activity in the manifest is really simple:
The theme needs to exist, and the parent must be one of the AppCompat provided themes.
Note: Any custom theme elements need to be defined twice, once for the compatibility version in the default namespace, one for the higher API levels in the android namespace.
Go to the Activity, and change it to extend
ActionBarActivity instead of
Add a custom namespace to the
menu.xml. Menu items need to be added using the support library, using the custom namespace.
And that’s it! You should be all set, be sure to give it a shot on both Froyo or Gingerbread devices as well as ICS and above. Here’s the full repo on github.