Creating Contextual Action Mode for single views in pre Honeycomb versions using Sherlock Library

August 2, 2012
By

We know that contextual  action mode is introduced in Android 3.0 ( Honeycomb ) and is not available in the pre Honeycomb versions of Android. But we can mimic a contextual action mode using Sherlock library. In this article we will develop a demo application containing contextual action mode for Android 2.x and above using Sherlock library.

In order to create contextual action mode using Android’s native library, see the article titled “Creating a contextual menu bar ( contextual action mode ) for a single view in Android“.

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


1. Setup Sherlock library in Eclipse

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


2. Create a new Android project namely “ContextualActionModeSingleViewSherlock”

New Android Project

Figure 1 : New Android Project


3. Design application launcher icon

Design Application Launcher

Figure 2 : Design Application Launcher


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 Support library from this project

Delete Android's support library which is added by ADT plugin automatically

Figure 5 : Delete Android's support library which is added by ADT plugin automatically


7. Add Sherlock Library to this project

  • Open Project Explorer
  • Right click the project   “ContextualActionModeSingleViewSherlock”
  • 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 Sherlock library to the project

Figure 6 : Add Sherlock library to the project


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


<resources>

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

    <string name="str_action1">Action1</string>
    <string name="str_action2">Action2</string>
    <string name="str_action3">Action3</string>

</resources>


9. Update the layout file res/layout/activity_main.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/tv_hello"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world"
        tools:context=".MainActivity" />

</RelativeLayout>


10. Create a new menu file res/menu/context_menu.xml


<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/action1"
        android:title="@string/str_action1"
    />

    <item
        android:id="@+id/action2"
        android:title="@string/str_action2"
        android:icon="@android:drawable/btn_star"
    />

    <item
        android:id="@+id/action3"
        android:title="@string/str_action3"
    />

</menu>


11. Update the class file src/in/wptrafficanalyzer/contextualactionmodesingleviewsherlock/MainActivity.java


package in.wptrafficanalyzer.contextualactionmodesingleviewsherlock;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.ActionMode;
import com.actionbarsherlock.view.ActionMode.Callback;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

public class MainActivity extends SherlockActivity {

    com.actionbarsherlock.view.ActionMode mMode;
    com.actionbarsherlock.view.ActionMode.Callback mCallback;

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

        TextView tvHello = (TextView) findViewById(R.id.tv_hello);

        mCallback = new Callback() {

            /** Invoked whenever the action mode is shown. This is invoked immediately after onCreateActionMode */
            @Override
            public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                return false;
            }

            /** Called when user exits action mode */
            @Override
            public void onDestroyActionMode(ActionMode mode) {
               mMode = null;
            }

            /** This is called when the action mode is created. This is called by startActionMode() */
            @Override
            public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                mode.setTitle("Demo");
                getSupportMenuInflater().inflate(R.menu.context_menu, menu);
                return true;
            }

            /** This is called when an item in the context menu is selected */
            @Override
            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                switch(item.getItemId()){
                    case R.id.action1:
                        Toast.makeText(getBaseContext(), "Selected Action1 ", Toast.LENGTH_LONG).show();
                        mode.finish();  // Automatically exists the action mode, when the user selects this action
                        break;
                    case R.id.action2:
                        Toast.makeText(getBaseContext(), "Selected Action2 ", Toast.LENGTH_LONG).show();
                        break;
                    case R.id.action3:
                        Toast.makeText(getBaseContext(), "Selected Action3 ", Toast.LENGTH_LONG).show();
                        break;
                }
                return false;
            }
        };

        OnLongClickListener listener = new OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {
                if(mMode!=null)
                    return false;
                else
                    mMode = startActionMode(mCallback);
                return true;
            }
        };

        tvHello.setOnLongClickListener(listener);
    }

    @Override
    public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
        getSupportMenuInflater().inflate(R.menu.activity_main, menu);
        return super.onCreateOptionsMenu(menu);
    }
}


12. Update the file AndroidManifest.xml to add Sherlock theme to the activity


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

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

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

            <activity
                android:name=".MainActivity"
                android:label="@string/title_activity_main"
                android:theme="@style/Theme.Sherlock"
            >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    </application>
</manifest>


13. Screenshot of the application in execution

Screenshot of the application in execution

Figure 7 : Screenshot of the application in execution


14. Download


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