2

I'm using a menu item on the action bar and I want to share my app by clicking the share icon. When I click the share icon it doesn't work. Also, I want to add text saying "install this app" when shared.

Here is my code:

private ShareActionProvider mShareActionProvider;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.mainpage, menu);
    MenuItem item = menu.findItem(R.id.menu_item_share);
    mShareActionProvider = (ShareActionProvider) item.getActionProvider();

    return true;
}

private void setShareIntent(Intent shareIntent) {
    if (mShareActionProvider != null) {
        mShareActionProvider.setShareIntent(shareIntent);
    }
}

Mainpage.xml menu:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/menu_item_share"
    android:showAsAction="ifRoom"
    android:title="Share"
    android:icon="@drawable/ic_store"
    android:actionProviderClass="android.widget.ShareActionProvider" />
</menu>
paulina_glab
  • 2,467
  • 2
  • 16
  • 25
Bachim01
  • 45
  • 2
  • 2
  • 9
  • 1
    So where are you calling `setShareIntent`? You need to call it at least once for the `ShareActionProvider` to be clickable. – ianhanniballake Jul 29 '13 at 16:04
  • sorry, i have no idea. do you have any suggestion on how to improve my code, all i want is get a clickable share icon on my action bar. – Bachim01 Jul 29 '13 at 16:12
  • @user2598397 you have an example @ android-sdk/samples/android-17/ApiDemos/src/com/example/android/apis/view/List16. http://stackoverflow.com/questions/17698596/checkable-relative-layout-as-item-in-multiselect-list/17698673#17698673 and this http://mobile.tutsplus.com/tutorials/android/android-sdk-implement-a-share-intent/ – Raghunandan Jul 29 '13 at 16:22

2 Answers2

18

If you want a static share Intent (i.e., it never changes), then you update your onCreateOptionsMenu to be

public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.mainpage, menu);
    MenuItem item = menu.findItem(R.id.menu_item_share);
    mShareActionProvider = (ShareActionProvider) item.getActionProvider();
    // Create the share Intent
    String playStoreLink = "https://play.google.com/store/apps/details?id=" +
        getPackageName();
    String yourShareText = "Install this app " + playStoreLink;
    Intent shareIntent = ShareCompat.IntentBuilder.from(this)
        .setType("text/plain").setText(yourShareText).getIntent();
    // Set the share Intent
    mShareActionProvider.setShareIntent(shareIntent);
    return true;
}
ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • I see the dark share icon instead of the white one...http://stackoverflow.com/questions/20601329/how-to-make-the-share-action-icon-white-instead-of-gray – Si8 Dec 16 '13 at 19:10
  • I am confused. You set the intent when the option menu is created. But if you use an action bar that happens very early. At a time when I don't yet have the data to share. – Martin Jan 08 '15 at 21:11
  • 1
    @Martin - yes, that's why I prefaced it with 'if you want a static share'. Otherwise just call `setShareIntent` with a new Intent whenever is appropriate for your app. – ianhanniballake Jan 08 '15 at 21:12
  • Thanks for the answer. Now appropriate would be when the user clicks the share button ;-). I see how I manager that. – Martin Jan 09 '15 at 18:11
  • 2
    @Martin - actually there isn't a listener on when the ShareActionProvider is clicked AFAIK - you should set the intent as soon as you have the data needed. – ianhanniballake Jan 09 '15 at 18:12
1

you can get the official tutorial here http://developer.android.com/guide/topics/ui/actionbar.html#ActionProvider

In the snappet you paste, you forget to call

mShareActionProvider.setShareIntent(intent);
hoot
  • 1,215
  • 14
  • 15