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

August 4, 2012
By

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


<resources>

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

</resources>


9. Create new layout file in 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>


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


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

    @Override
    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="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.actionbarcustomactionprovidersherlock.SearchActionProvider"
    />
</menu>

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


package in.wptrafficanalyzer.actionbarcustomactionprovidersherlock;

import android.os.Bundle;

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

public class MainActivity extends SherlockActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getSupportMenuInflater().inflate(R.menu.items, menu);
        return super.onCreateOptionsMenu(menu);
    }
}


13. Update the file AndroidManifest.xml


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

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.Sherlock" >

        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >

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

</manifest>

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

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