I\'m trying to make \"Share\" button in Action Bar of Android Application. Here my code:
import android.content.Intent;
import android.os.Bundle;
import andr
First, you cannot use android.widget.ShareActionProvider
with the appcompat-v7
action bar backport (e.g., ActionBarActivity
). Either use the appcompat-v7
version of ShareActionProvider
, or move everything over to the native action bar.
Second, if you stick with appcompat-v7
, then you cannot safely use getActionProvider()
, as that method will not exist on API Level 10 and below. Replace menuItem.getActionProvider()
with MenuItemCompat.getActionProvider(menuItem)
.
FWIW, here is a sample project that implements the appcompat-v7
edition of ShareActionProvider
.
You can follow the pattern in Code Sample from Google in link below. https://github.com/googlesamples/android-ActionBarCompat-ShareActionProvider
The easiest way is to go to your Android Studio => File, Import Sample. Then type in "Share Action Provider".
Below are the code involved in creating Share Action Menu Item with ShareActionProvider with ActionBarCompat, backwards compatible to API v7.
MainActivity.java
// BEGIN_INCLUDE(get_sap)
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu resource
getMenuInflater().inflate(R.menu.main_menu, menu);
// Retrieve the share menu item
MenuItem shareItem = menu.findItem(R.id.menu_share);
// Now get the ShareActionProvider from the item
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareItem);
// Get the ViewPager's current item position and set its ShareIntent.
int currentViewPagerItem = ((ViewPager) findViewById(R.id.viewpager)).getCurrentItem();
setShareIntent(currentViewPagerItem);
return super.onCreateOptionsMenu(menu);
}
// END_INCLUDE(get_sap
private void setShareIntent(int position) {
// BEGIN_INCLUDE(update_sap)
if (mShareActionProvider != null) {
// Get the currently selected item, and retrieve it's share intent
ContentItem item = mItems.get(position);
Intent shareIntent = item.getShareIntent(MainActivity.this);
// Now update the ShareActionProvider with the new share intent
mShareActionProvider.setShareIntent(shareIntent);
}
// END_INCLUDE(update_sap)
}
main_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:support="http://schemas.android.com/apk/res-auto">
<!--
To use ShareActionProvider provided by ActionBarCompat, we reference the class by set the
support:actionProviderClass attribute with the full class name of ShareActionProvider.
-->
<item
android:id="@+id/menu_share"
android:title="@string/menu_share"
support:actionProviderClass="android.support.v7.widget.ShareActionProvider"
support:showAsAction="always" />
Follow following steps:
Step.1) To add a "share" action to your activity, put a ShareActionProvider
in the app bar's menu resource. Like this
<item android:id="@+id/action_share"
android:title="@string/share"
app:showAsAction="ifRoom"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider"/>
Please note the actionProviderClass
Step.2) Now you make sure that in your Activity Java Class you import same ShareActionProvider
import android.support.v7.widget.ShareActionProvider;
Step.3)
Below section for onCreate
methond
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_bar, menu);
ShareActionProvider shareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(menu.findItem(R.id.actionbar_share));
shareActionProvider.setShareIntent(shareIt());
Intent method
private Intent shareIt() {
Intent intent= new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
String shareMsgBody = "Hello, Share this with world !!";
intent.putExtra(Intent.EXTRA_TEXT, shareAndPromotionBody);
startActivity(Intent.createChooser(intent, "Spread the message!!"));
return intent;
}