Monday, August 5, 2013

Using Action Bar on Android 2.1 (API Level 7) and above using Support Library (Rev. 18) in Android Studio 0.2.3

The time has come where we can finally use the Action Bar for Android 2.1 and above without using 3rd Party library such as the Action Bar Sherlock.

Google has released revision 18 of the Support Library where it allows you to implement Action Bar on older Android versions.

To get started, It is better to update your Android Studio to the latest version (mine is 0.2.3 at the time I wrote this blog).

First thing you have to do is to create a project or if you have already an existing project open your project.

Update/Install Support Package

The next thing we need to do is to make sure that we have the latest Support Library (at this time, it will be revision 18).

Open the SDK Manager using the toolbar in the Android Studio


In the Android SDK Manager, tick the Android Support Library and on the Status column, you can see that the latest version is Revision 18. Install the package.


After updating the Support Library, restart the Android Studio and re-open the project again.

Configure Gradle Build file to use the latest Support Library

If you have updated the Support Library before creating a project, then it should use the latest support library in the Gradle Build file, otherwise we have to modify it to use the latest one.

Modify the inner build.gradle file inside the project folder. Take note that you may see two build.gradle file. You have to modify the inner most build.gradle file as shown below in my project.


In the dependencies section, replace the line with compile com.android.support:appcompat-v7:18.0.+. In my build script, I just commented it out so you can see the previous value.

If you don't have the dependencies section, then just add it as shown below.


For some reason, I have to restart the Android Studio to reflect the changes I have done in build.gradle. After restart, the support library will be shown in the External Libraries.



Modify the Activity to have the Action Bar

Open the activity you want to have the Action Bar, in my example MainActivity. Extend the activity to ActionBarActivity. This will import package android.support.v7.app.ActionBarActivity.


Modify AndroidManifest.xml to use Theme.AppCompat themes

The last thing we need to setup is the theme resources needed by Action Bar. Without this step, the activity will trigger a runtime exception as shown below.


Open the AndroidManifest.xml file and add the line android:theme="@style/Theme.AppCompat.Light".


Updated on 06 Aug 2013 : If you have notice, Android Studio cannot resolve the theme that we have added. As a work around, you can copy the themes.xml and themes_base.xml from the SDK folder. This folder may vary depending on your OS or system configuration.

C:\Documents and Settings\Your.User.Name\Local Settings\Application Data\Android\android-studio\sdk\extras\android\support\v7\appcompat\res\values



After copying the xml files, Android Studio should be able to resolve them as shown below.



Rebuild the project and run the application.

Application running in Android 2.1 emulator using Action Bar in the support library.



I hope this helps you to implement Action Bar on Android 2.1 using the support library.



No comments:

Post a Comment