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.



Thursday, July 4, 2013

Using ActionBarSherlock in Android Studio (0.1.9)

Update 05 Aug 2013 : A better way to use Action Bar on Android 2.1 and above, see Using Action Bar on Android 2.1 (API Level 7) and above using Support Library (Rev. 18) in Android Studio 0.2.3

Preparing Android Studio Packages

Make sure the following packages are installed:


Preparing the Action Bar Sherlock Library

Download the Action Bar Sherlock library from https://api.github.com/repos/JakeWharton/ActionBarSherlock/zipball/4.3.1 or go to http://actionbarsherlock.com/ to get the latest version.

Creating the Android Project

Create a project in Android Studio called YourProject. Click Next to continue until you have created the project.




Your Project Root directory should look like this:


Importing Action Bar Sherlock Library to your Project

Open the Action Bar Sherlock Library you have downloaded earlier and extract it somewhere (for example to your Desktop). After extraction, the directory should look like this:


Copy the directory called "actionbarsherlock" to your Project Root directory. After copying, your Project Root directory should look like this:


Fixing your Gradles

First, modify the settings.gradle to tell Gradle that we have to include another project on our build. settings.gradle is located on your Project Root (YourApplicationProject/settings.gradle).

Let us include the actionbarsherlock directory in your settings.gradle:


Now that we have included the actionbarsherlock directory, we need to create a build.gradle file in the actionbarsherlock directory. We can copy the build.gradle file from our Application Directory (Note that there are two build.gradle file currently in our Project Directory. One is located at the Project Root Directory YourApplicationProject/build.gradle, another one is located at the Application Directory YourApplicationProject/YourApplication/build.gradle. The later is what we need to copy to YourApplicationProject/actionbarsherlock).

After copying, the actionbarsherlock directory should look like this:


Modify the copied build.gradle from the actionbarshelock directory. For the first change, plugin is changed from android to android-library. The second change will tell Gradle that this library is needed and include it once as compared to the original code where it will always include the library file even though it might have been included already on other build.gradle. This could cause issues when compiling the project. The last code block tells Gradle where are the relevant files and directory for it to compile the library.



Lastly, we need to modify the build.gradle from our Application Directory (YourApplicationProject/YourApplication/build.gradle) to include the actionbarsherlock library project and replace the compile file line with compile to prevent the errors when including libraries more than once.


Now your Gradle are ready to compile. But before you can start coding, we need to configure Android Studio and tell about the library.

Adding Action Bar Sherlock library to Android Studio

Go to File then Project Structure. In Project Settings, click the Modules from the list and then click the "+" plus sign on the next list as seen below:



Choose Import Module and Select the actionbarsherlock directory. Click OK when finished. On the Import Module dialog box, click Next until finished. Apply and then OK on the Project Structure Dialog Box. In Android Studio 0.1.9, I have to restart the IDE for the modules to appear in the List.

After restarting, Go to File then Project Structure again, and then select YourApplication from the list. On the right side, select Dependencies tab and click the "+" sign and choose Module Dependency.



Select the actionbarsherlock and then click Apply then OK to quit the Project Structure. Restart again the Android Studio.

You can now start coding your project using the Action Bar Sherlock.

Testing the Action Bar Sherlock Library

In MainAcitivity.java, lets change the Activity to SherlockActivity. We need to remove the onCreateOptionsMenu override method as this does not exists.


Finally we need to modify the AndroidManifest.xml of our application to change the theme to the Action Bar Sherlock library themes.


Your application will now able to run the Action Bar Sherlock library for Android 2.X and above. This is an emulator running on Android 2.2:

I hope this helps you to get started with Action Bar Sherlock Library in Android Studio (0.1.9).

Updated on 2013/07/16 : Here is the source code for the whole project in a zip file.