Creating Custom Action Provider in Action Bar

July 22, 2012

In the article titled “Adding Custom Action View to Action Bar in Android“, we have seen how to add widgets such as edittext in to actionbar. In this article we will do the same thing but in a different method that is using action provider. The  advantage of using action provider instead of action view is that , the action provider provides more modularity, less coupling and code reusability.


See the article titled “An expandable action using Custom Action Provider in pre Honeycomb versions using Sherlock library“, for creating Custom action provider in pre Honeycomb android versions.

The application described here is developed in Eclipse 3.7.2 and tested in Android Virtual Device with the API level 14.

1. Create a new Android Project namely “ActionBarCustomActionProvider”

New Android Project

Figure 1 : New Android Project

2. Select Android build target

Select Build Target

Figure 2 : Select Build Target

3. Enter application details

Enter Application Details

Figure 3 : Enter Application Details

4. res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?>

    <string name="hello">Hello World, MainActivity!</string>
    <string name="app_name">ActionBarCustomActionProvider</string>
    <string name="str_search">Search</string>


5. res/menu/items.xml

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

6. res/layout/search_layout.xml

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


7. src/in/wptrafficanalyzer/actionbarcustomactionprovider/

package in.wptrafficanalyzer.actionbarcustomactionprovider;

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

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;

8. src/in/wptrafficanalyzer/actionbarcustomactionprovider/

package in.wptrafficanalyzer.actionbarcustomactionprovider;

import android.os.Bundle;
import android.view.Menu;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {

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

9. Application in Execution


Displaying EditText widget in Action Bar using Custom Action Provider

Figure 4: Displaying EditText widget in Action Bar using Custom Action Provider

10. Download


11. 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