Implementing ActionBarSherlock in Android

In this tutorial, you will learn how to implement ActionBarSherlock into your Android Application. ActionBarSherlock automatically uses the native action bar when appropriate or wrap a custom implementation around your layouts. Using ActionBarSherlock allows you to easily develop an application with an action bar for every version of Android from 2.x and up. So lets begin…

Download ActionBarSherlock Library

Visit ActionBarSherlock website and download the Zip file provided. The extracted zip folder should contain the files shown below.

ActionBarSherlock Folder

Rename the folder “actionbarsherlock” to “ActionBarSherlockLib” and copy the folder into your workspace as shown on the screenshot below.

Insert ActionBarSherlock into workspace

Import ActionBarSherlock Library

To import ActionBarSherlock Library into Eclipse, go to File > Import > Select Existing Android Code into Workspace.

Click on Browse button and locate ActionBarSherlock Library in your workspace. Import ActionBarSherlock Library into your workspace by ticking the check box as shown on the screenshot below.

Import ActionBarSherlock Library

On successful import, you should have ActionBarSherlock Library in your Package Explorer as shown on the screenshot below.

Package Explorer ActionBarSherlock

To clear the errors, right click on the project and select Properties. Select Java Compiler and change the “Compiler compliance level” to 1.6. 

Eclipse Java Compiler

Implement ActionBarSherlock Library into Project

Create a new project in Eclipse File > New > Android Application Project. Fill in the details and name your project ActionBarSherlockTutorial.

Application Name : ActionBarSherlockTutorial

Project Name : ActionBarSherlockTutorial

Package Name : com.androidbegin.actionbarsherlocktutorial

Next, import ActionBarSherlock Library into your project. Go to your project properties by right clicking on your project > Properties > Android > Add > Select ActionBarSherlockLib > Apply > OK.

Add ActionBarSherlock Library

Open your MainActivity.java and paste the following code.

MainActivity.java

package com.androidbegin.actionbarsherlocktutorial;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.MenuItem.OnMenuItemClickListener;

import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends SherlockActivity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Get the view from activity_main.xml
		setContentView(R.layout.activity_main);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// First Menu Button
		menu.add("Help")
				.setOnMenuItemClickListener(this.HelpButtonClickListener)
				.setIcon(R.drawable.help_button) // Set the menu icon
				.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

		// Second Menu Button
		menu.add("Like")
				.setOnMenuItemClickListener(this.LikeButtonClickListener)
				.setIcon(R.drawable.like_button) // Set the menu icon
				.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

		// Third Menu Button
		menu.add("Exit")
				.setOnMenuItemClickListener(this.ExitButtonClickListener)
				.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

		return super.onCreateOptionsMenu(menu);
	}

	// Capture first menu button click
	OnMenuItemClickListener HelpButtonClickListener = new OnMenuItemClickListener() {

		public boolean onMenuItemClick(MenuItem item) {

			// Create a simple toast message
			Toast.makeText(MainActivity.this, "Help Button", Toast.LENGTH_SHORT)
					.show();

			// Do something else
			return false;
		}
	};

	// Capture second menu button click
	OnMenuItemClickListener LikeButtonClickListener = new OnMenuItemClickListener() {

		public boolean onMenuItemClick(MenuItem item) {
			// Create a simple toast message
			Toast.makeText(MainActivity.this, "Like Button", Toast.LENGTH_SHORT)
					.show();

			// Do something else
			return false;
		}
	};

	// Capture third menu button click
	OnMenuItemClickListener ExitButtonClickListener = new OnMenuItemClickListener() {

		public boolean onMenuItemClick(MenuItem item) {
			// Create a simple toast message
			Toast.makeText(MainActivity.this, "Exit Button", Toast.LENGTH_SHORT)
					.show();

			// Do something else
			return false;
		}
	};
}

We have created three actionbar menus with the codes above and on each menu click will show a simple toast message. You can perform other tasks such as showing an alert dialog, start an intent or place actions that have a global impact on the application.

We have prepared some sample icons for the menus. Insert your downloaded sample icons into your res drawable-hdpi.

Sample Icons

[wpfilebase tag=file id=82 tpl=download-button /]

Next, create an XML file for your MainActivity Graphical Layout. Go to res > layout > Right Click on layout > New > Android XML File

Name your new XML file activity_main.xml and paste the following code.

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/ActionBarSherlock" />

</RelativeLayout>

Next, change the application name and other texts. Open your strings.xml in your res > values folder and paste the following code.
strings.xml

<resources>

    <string name="app_name">ActionBarSherlock Tutorial</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="ActionBarSherlock">ActionBarSherlock Tutorial</string>

</resources>

In your AndroidManifest.xml, we need to change the theme style to “Theme.Sherlock“. Insert “splitActionBarWhenNarrow” to your activity to show actionbar at the bottom of the screen. Open your AndroidManifest.xml and paste the following code.

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.androidbegin.actionbarsherlocktutorial"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="1"
        android:targetSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.Sherlock" >
        <activity
            android:name=".MainActivity"
            android:uiOptions="splitActionBarWhenNarrow" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Output:

ActionBarSherlock Tutorial ScreenShots

Source Code 

[purchase_link id=”7842″ text=”Purchase to Download Source Code” style=”button” color=”green”]

Latest comments

there is a lot of errors i got once i implement that actionbarsherlock project as a libraryinto one of my eclipse project. What should I do to resolve that case?Because the old support v4 is already removed from my project directory.

gumuruh

Implementing ActionBarSherlock in Android

Hi , in the androidmanifest whene i tried to change the Theme adding Eclipse told me that Theme.Sherlock not found !!!

Yns Abbassi

Implementing ActionBarSherlock in Android

remove support jar file from your application's libs folder. It's already added to the library's libs

Rowan Tarek

Implementing ActionBarSherlock in Android

Hi!What is the difference between simple action bar and actionbar Sherlock? I have used action bar without ActionbarSherlock library and it works fine. is there any extra functionality in this library?

Ubaid Gul

Implementing ActionBarSherlock in Android