This example explains how to implement effective share action in your actionbar using ShareActionProvider class. ActionProvider is made available since Android 4.0 (API Level 14), But to have a share widget in older versions (Android 2.1 and higher) we will use appcompat library. An ActionProvider, once attached to a menu item in the action bar, handles both the appearance and behavior of that item. In the case of ShareActionProvider, you provide a share intent and it does the rest.
Specifying The Action Provider Class
To use the ShareActionProvider, add http://schemas.android.com/apk/res-auto schema to your menu items XML. set the actionProviderClass attribute on a menu item in the XML for the Action Bar’s menu.
<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" >
<item
android:id="@+id/action_share"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider"
app:showAsAction="always"
android:title="Share" />
</menu>
Update your mainActivity to create shareActionProvider
import android.content.Intent; import android.os.Bundle; import android.support.v4.view.MenuItemCompat; import android.support.v7.app.ActionBarActivity; import android.support.v7.widget.ShareActionProvider; import android.view.Menu; import android.view.MenuItem; public class MainActivity extends ActionBarActivity { private ShareActionProvider mShareActionProvider; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); // Get the menu item. MenuItem menuItem = menu.findItem(R.id.action_share); // Get the provider and hold onto it to set/change the share intent. mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem); // Set share Intent. // Note: You can set the share Intent afterwords if you don't want to set it right now. mShareActionProvider.setShareIntent(createShareIntent()); return true; } // Create and return the Share Intent private Intent createShareIntent() { Intent shareIntent = new Intent(Intent.ACTION_SEND); shareIntent.setType("text/plain"); shareIntent.putExtra(Intent.EXTRA_TEXT, "http://droiddevguide.blogspot.ae/"); return shareIntent; } // Sets new share Intent. // Use this method to change or set Share Intent in your Activity Lifecycle. private void changeShareIntent(Intent shareIntent) { mShareActionProvider.setShareIntent(shareIntent); } }
Output:
Can you please example make it using appcompat library also so work for both older and new version. Its help me. Thanks
ReplyDeleteAbove example uses AppCompat library. This example works for Android 2.1 and above.
DeleteThank so much :)
ReplyDeletethank so much very easy to understand..
ReplyDeletebut still i stuffed bcoz i want to share more than one string ..
i can share more than one string in single putExtra() but string returns null
if you have any idea about it plz share
Thank you
ReplyDeleteThanks bro, was very useful!!
ReplyDeleteFinally, an example that works right away -- thank you!
ReplyDeletetitanium flat iron - Tithon, Taiwan
ReplyDeleteTitanium implant grade titanium earrings Iron is the main ingredient of black titanium rings the babyliss titanium flat iron Tithon gold rush. Its popularity is titanium expensive in Taiwan has increased in the years since the launch of titanium white wheels its trademark
uv981 timberland botas hombre,hokajapan,vivobarefoot ישראל,chacos stockists australia,aldo shoes australia ,timberlandsuisse,etnies trainers,pit viper briller,gabor boots ireland eu597
ReplyDeleteem857 castañer alpargatas mujer,stoneislandmikina,rei co-op 650 down jacket,rab capacitor,longchamp sac homme,longchamp çanta trendyol,stoneislandmagyarorszag,rab equipment,longchampaustralia rg187
ReplyDelete