Creating Custom Action Provider in Action Bar

July 22, 2012
By

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"?>
<resources>

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

</resources>


5. res/menu/items.xml


<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/mnu_search"
        android:title="@string/str_search"
        android:showAsAction="ifRoom|withText|collapseActionView"
        android:actionProviderClass="in.wptrafficanalyzer.actionbarcustomactionprovider.SearchActionProvider"
    />
</menu>


6. res/layout/search_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <EditText
        android:id="@+id/edt_search"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="text"
    />
</RelativeLayout>


7. src/in/wptrafficanalyzer/actionbarcustomactionprovider/SearchActionProvider.java


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) {
        super(context);
        mContext = context;
    }

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


8. src/in/wptrafficanalyzer/actionbarcustomactionprovider/MainActivity.java


package in.wptrafficanalyzer.actionbarcustomactionprovider;

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

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.items, 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

http://developer.android.com/guide/index.html


How to hire me?

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

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

For hiring me, please mail your requirements to info@wptrafficanalyzer.in.

My other blogs
store4js.blogspot.com


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