An expandable action using Custom Action Provider in pre Honeycomb versions using Sherlock library

August 4, 2012

This article is an extension to the article “Creating Custom Action Provider in Action Bar“. In this article we will see , how to add action views to action bars using custom action provider in pre Honeycomb android versions.

The application discussed here is developed in Eclipse ( 3.7.2 ) with ADT plugin ( 20.0.2 ) and Android SDK platform ( R20.0.1 ) .

1. Setup Action Bar Sherlock ( ABS ) library in Eclipse IDE

Please see the article titled “Setting up Sherlock library for action bar in pre Honeycomb Android Applications

2. Create new Android application project

New Android Application Project

Figure 1 : New Android Application Project

3. Design application launcher icon

Design Application launcher Icon

Figure 2 : Design Application launcher Icon

4. Create a blank activity

Create a blank activity

Figure 3 : Create a blank activity

5. Enter MainActivity details

Enter MainActivity Details

Figure 4 : Enter MainActivity Details

6. Delete Android’s default backward compatibility support library from this project

Delete Android's Default backward compatibility support library from the project

Figure 5 : Delete Android's Default backward compatibility support library from the project

7. Add Action Bar Sherlock library to this project

  • Open Project Explorer
  • Right click the project
  • Open Properties
  • Select “Android” tab from the Properties Window
  • Then the given below screen will be appeared
  • Using Add button, add the Sherlock library to this project
Add ABS to this project

Figure 6 : Add ABS to this project

8. Update the file res/values/strings.xml


    <string name="app_name">ActionBarCustomActionProviderSherlock</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_main">MainActivity</string>

    <string name="str_search">Search</string>


9. Create new layout file in res/layout/search_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""
    android:orientation="horizontal" >



10. Define custom action provider class in the file src/in/wptrafficanalyzer/actionbarcustomactionprovidersherlock/

package in.wptrafficanalyzer.actionbarcustomactionprovidersherlock;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;

import com.actionbarsherlock.view.ActionProvider;

public class SearchActionProvider extends ActionProvider{
    Context mContext;

    public SearchActionProvider(Context context) {
        mContext = context;

    public View onCreateActionView() {
        LayoutInflater layoutInflater = LayoutInflater.from(mContext);
        View view = layoutInflater.inflate(R.layout.search_layout,null);
        return view;

11. Create a new menu resource file res/menu/items.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="" >

12. Update the MainActivity class file in src/in/wptrafficanalyzer/actionbarcustomactionprovidersherlock/

package in.wptrafficanalyzer.actionbarcustomactionprovidersherlock;

import android.os.Bundle;

import com.actionbarsherlock.view.Menu;

public class MainActivity extends SherlockActivity {

    public void onCreate(Bundle savedInstanceState) {

    public boolean onCreateOptionsMenu(Menu menu) {
        getSupportMenuInflater().inflate(, menu);
        return super.onCreateOptionsMenu(menu);

13. Update the file AndroidManifest.xml

<manifest xmlns:android=""
    android:versionName="1.0" >

        android:targetSdkVersion="15" />

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

            android:label="@string/title_activity_main" >

                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />


14. Screenshots of the application in execution

Screenshot of the MainActivity Screen

Figure 7 : Screenshot of the MainActivity Screen

Action bar with Custom action view using Custom Action Provider

Figure 8 : Action bar with expandable action view using Custom Action Provider

15. Download

16. Reference

How to hire me?

I am George Mathew, working as software architect and Android app developer at

You can hire me on hourly basis or on project basis for Android applications development.

For hiring me, please mail your requirements to

My other blogs

Android Knowledge Quiz

Ready to test your knowledge in Android? Take this quiz :

Tags: , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

Be friend at g+

Subscribe for Lastest Updates

FBFPowered by ®Google Feedburner