Adding ShareActionProvider to action bar in pre Honeycomb versions using Sherlock library

August 4, 2012
By

ShareActionProvider is a simple means to place a “Share” action button in an application’s activity. In the article titled “Using ShareActionProvider in Action Bar“, we have added a “Share” action button to the action bar for Honeycomb and its higher Android versions. In this article we will see how to add “Share” button to action bar for pre Honeycomb versions using Sherlock library.

This application is developed in Eclipse ( 3.7.2) with ADT plugin (20.0.2) and Android SDK ( R20.0.1 ).


1. Setting up Action Bar Sherlock library in Eclipse IDE

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


2. Create a new Android application project

New Android Application Project

Figure 1 : New Android Application Project


3. Design the application launcher icon

Designing Application Launcher Icon

Figure 2 : Designing 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 support library from the project

Delete Support Library

Figure 5 : Delete Support Library


7. Add ActionBarSherlock 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 ActionBar Sherlock Library

Figure 6 : Add ActionBar Sherlock Library


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


<resources>

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

    <string name="share">Share</string>

</resources>


9. Create a new menu 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/share"
        android:title="@string/share"
        android:showAsAction="ifRoom"
        android:actionProviderClass="com.actionbarsherlock.widget.ShareActionProvider"
    />
</menu>


10. Update the MainActivity class file src/in/wptrafficanalyzer/ActionBarSherlockShareActionProvider”

package in.wptrafficanalyzer.actionbarsherlockshareactionprovider;

import android.content.Intent;
import android.os.Bundle;

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

public class MainActivity extends SherlockActivity {

    private ShareActionProvider mShareActionProvider;

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        /** Inflating the current activity's menu with res/menu/items.xml */
        getSupportMenuInflater().inflate(R.menu.items, menu);

        /** Getting the actionprovider associated with the menu item whose id is share */
        mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.share).getActionProvider();

        /** Getting the target intent */
        Intent intent = getDefaultShareIntent();

        /** Setting a share intent */
        if(intent!=null)
            mShareActionProvider.setShareIntent(intent);
        return super.onCreateOptionsMenu(menu);

    }

    /** Returns a share intent */
    private Intent getDefaultShareIntent(){

        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_SUBJECT, "SUBJECT");
        intent.putExtra(Intent.EXTRA_TEXT,"Sample Content !!!");
        return intent;
    }
}


11. Update the xml file AndroidManfest.xml


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="in.wptrafficanalyzer.actionbarsherlockshareactionprovider"
    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>


12. Screenshot of the application in execution

Share action in Action Bar

Figure 7 : Executing the application in Android x86 platform


13. Download


14. 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: , , , , , ,

7 Responses to Adding ShareActionProvider to action bar in pre Honeycomb versions using Sherlock library

  1. Lshanu Near Kheel on September 25, 2012 at 8:41 pm

    Why is it when I run it my emulator, there’s no display like that screenshot that there is a drop down list like google+, bluetooth and gmail? Please have a clarification as soon as possible. I’m badly needing this stuff.

    • george on September 25, 2012 at 10:29 pm

      Because those applications are not available in your emulator. The screenshot shows the application executed in Android x86 platform.

  2. Lshanu Near Kheel on September 27, 2012 at 1:27 am

    Ah okay! Thanks for taking time to answer. Then how am I going to implement it with the same as spinner? Spinners you know, that if you insert values in a spinner the one that is on the first drop down list will be displayed twice if you click a certain list. But the problem is I want it fully customize.. Like I will have to use a string as basis to make it more efficient like this the one you have posted. I want it to fully customize. The question is, how can I do that? Hope you understand what I’m trying to explain..

    Thank you for your time in reading and answering my queries… (^_^)

  3. Lshanu Near Kheel on September 27, 2012 at 1:36 am

    One more thing, How can I determine if it’s Android (x86) or something else? Please…

  4. parashuram on December 12, 2012 at 10:05 pm

    After selecting messaging or Bluetooth etc, it will be placed in action bar. How to make appear only in drop down ?

  5. santhosh on May 24, 2013 at 11:33 am

    After selecting messaging or Bluetooth etc, it will be placed in action bar. How to make appear only in drop down ?

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