The following code shows how to Use ShareActionProvider.
Main layout xml file
<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:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="hello_world" tools:context=".MainActivity" /> </RelativeLayout>
Menu xml file
<menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/menu_settings" android:showAsAction="never" android:title="Settings" /> <item android:id="@+id/menu_share" android:showAsAction="ifRoom" android:title="Share" android:actionProviderClass="android.widget.ShareActionProvider" /> </menu>
Main Activity Java code
package com.java2s.myapplication3.app; // w w w . j a v a 2 s. c o m import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.ShareActionProvider; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); //Find the item and set the share Intent MenuItem item = menu.findItem(R.id.menu_share); ShareActionProvider provider = (ShareActionProvider) item.getActionProvider(); Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("text/plain"); intent.putExtra(Intent.EXTRA_TEXT, "Text Sharing Update"); provider.setShareIntent(intent); return true; } }