Android ShareActionProvider

When you build application that allows the user to share a simple text or file to other applications, this Android tip is useful to you. In this tip, I am going to show you how to achieve this goal using the ShareActionProvider. ShareActionProvider displays share actions to the user on the action bar. Once you attached the ShareActionProvider to the action bar, it handles both the appearance and behavior of that item. To get started with the ShareActionProvider, you first define android:actionProviderClass attribute for the corresponding <item> in the menu layout file. For an example, i created a menu layout file called menu.xml in the res/menu folder.


<menu xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   tools:context="com.example.andexample.MainActivity" >

   <item android:id="@+id/menu_item_share"
     android:orderInCategory="100"
     android:showAsAction="ifRoom"
     android:title="Share"
     android:actionProviderClass="android.widget.ShareActionProvider"
     />
  

</menu>


Next, in the onCreateOptionsMenu() method of the MainActivity class that extends the Activity class, you need to inflate the menu.xml file, get ShareActionProvider object from the menu item, and set a share intent to the ShareActionProvider object. When you set the intent object to the ShareActionProvider, applications that match the ACTION_SEND action and the mine type specified by the setType() method will be displayed. So, you can select one of the applications to send your data.

public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu, menu);
MenuItem item=menu.findItem(R.id.menu_item_share);
ShareActionProvider mAcPro=(ShareActionProvider)item.getActionProvider();
if(mAcPro!=null){
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, "This text is to be shared to other applications.");
shareIntent.setType("text/plain");
mAcPro.setShareIntent(shareIntent);
}

return super.onCreateOptionsMenu(menu);
}


The approach to display share actions on the action bar discussed about only works in API level 14 (Android 4.0) and higher. So, you need to change the android:minSdkVersion in the AndroidManifest.xml file to meet this limitation to avoid incompatible API error.
If you want to make the ShareActionProvider to work on Android lower than level 14, you will use the support library v7. First, you need to change the menu.xml file to the one shown below.

<menu xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:yourapp="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   tools:context="com.example.andexample.MainActivity" >

   <item android:id="@+id/menu_item_share"
     android:orderInCategory="100"
     yourapp:showAsAction="ifRoom"
     android:title="Share"
     yourapp:actionProviderClass="android.support.v7.widget.ShareActionProvider"
     />  

</menu>


Then, in the onCreateOptionsMenu() method, you need to change from the line: ShareActionProvider mAcPro=(ShareActionProvider)item.getActionProvider(); to this line of code:

ShareActionProvider mAcPro=(ShareActionProvider) MenuItemCompat.getActionProvider(item);

Also, you need to import android.support.v7.widget.ShareActionProvider instead of android.widget.ShareActionProvider. The MainActivity class has to extend the ActionBarActivity class instead of Activity class.

Android-ShareActionProvider

Posted by: Dara | post date: 10-16-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.