Introduction to ActionBarSherlock

ActionBarSherlock is an Android library which is used to create the ActionBar feature in your application.
We can develop applications with action bar for all versions of Android starting from 2.X and above.

How to implement ActionBarSherlock in application:
Let’s see how we get this feature in our application.

STEPS
�? Get the library ActionBarSherlock. from the official website (after download extract the zip file, take the actionbarsherlock file only).
�? Import this library (ActionBarSherlockLib after rename) to your workspace.
�? Java Compiler compliance level should be 1.6.
�? Now, Create an Application ActionBarSherlockExampleAndroid.
�? Then, add the ActionBarSherlockLib library in your application.
�? Make sure both are in the same workspace.

After adding the library let’s do it with the help of example:
Here in this example, it shows action bar having like button, help button, and exit button and main content shows a simple textview.

Requirements
MainActvity.java, activity_main.xml .

activity_main.xml
This is our main layout file, in this example, it shows only TextView. We can customize it according to the need of an application.
Add the code

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

MainActivity.java:
Extends this activity with SherlockActivity. Take the view from activity_main.xml file in onCreate() method.
Add the code in onCreateOptionsMenu() method{…}
Using this method, we can create the menu items for action bar like help button, like button and exit.

Help Button

menu.add("Help")
.setOnMenuItemClickListener(this.HelpButtonClickListener)
.setIcon(R.drawable.help_button
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

Like Button

menu.add("Like")
.setOnMenuItemClickListener(this.LikeButtonClickListener)
.setIcon(R.drawable.like_button)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

Exit Button

menu.add("Exit")
.setOnMenuItemClickListener(this.ExitButtonClickListener)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

Here we add these buttons in the menu and set listeners and icons on them.
After clicking on these icons, it shows the toast.

On Help Button click

OnMenuItemClickListener HelpButtonClickListener = new OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(MainActivity.this, "Help Button", Toast.LENGTH_SHORT)
.show();
return false;
}
};

On Like Button click

OnMenuItemClickListener LikeButtonClickListener = new OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(MainActivity.this, "Like Button", Toast.LENGTH_SHORT)
.show();
return false;
}
};

On Exit Button click

OnMenuItemClickListener ExitButtonClickListener = new OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(MainActivity.this, "Exit Button", Toast.LENGTH_SHORT)
.show();
return false;
}
};

To change the theme and action bar place, we have to do small changes in AndroidManifest.xml like:

android:theme="@style/Theme.Sherlock"
android:uiOptions="splitActionBarWhenNarrow" >

Simply we can add these options on actionbarsherlock.

Output

Download Code: GitHub

Conclusion

ActionBarSherlock is just an extension of the support library which is used to create the ActionBar feature in the application. We have to add this library in our application. We can easily develop applications and customize it for every version of Android from 2.x and above.
Keep visiting www.acadgild.com for more updates on the courses.

 

 

Tags

 Â