0

I am creating an android app in which I have all ready added a share option to share content of the app, but I want to add another share option which will be able to share app download link (share this app), both the options use on create option menu, can anyone please tell me if it is possible to add two on create option or is there other way to add second share action. Following is the code I have used for "share this app" action.

 private ShareActionProvider mShareActionProvider;
@Override
public boolean onCreateOptionsMenu(Menu menu) {

    /** Inflating the current activity's menu with res/menu/items.xml */
    getMenuInflater().inflate(R.menu.menu_main, menu);

    /** Getting the actionprovider associated with the menu item whose id is share */
    mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.share).getActionProvider();

    /** Setting a share intent */
    mShareActionProvider.setShareIntent(getDefaultShareIntent());

    return super.onCreateOptionsMenu(menu);

}

/** Returns a share intent */
private Intent getDefaultShareIntent(){
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");
    intent.putExtra(Intent.EXTRA_SUBJECT, "download the app");
    intent.putExtra(Intent.EXTRA_TEXT," play.google.com ");
    return intent;
}

menu_main

<item
    android:title="Share"
    android:id="@+id/menu_item_share"
    android:showAsAction="ifRoom"
    android:icon="@drawable/share"
    />

<item
    android:id="@+id/share_this_app"
    android:title="share this app"
    android:showAsAction="never"
    android:actionProviderClass="android.widget.ShareActionProvider"/>
comrade
  • 4,590
  • 5
  • 33
  • 48
sagar chaudhary
  • 33
  • 1
  • 12

1 Answers1

0

Have you tried something like this? Let me know if it works.

private ShareActionProvider mShareActionProvider;
private ShareActionProvider mShareActionProvider2;

@Override
public boolean onCreateOptionsMenu(Menu menu) {    
    /** Inflating the current activity's menu with res/menu/items.xml */
    getMenuInflater().inflate(R.menu.menu_main, menu);

    /** Getting the actionprovider associated with the menu item whose id is share */
    mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.menu_item_share).getActionProvider();
    mShareActionProvider2 = (ShareActionProvider) menu.findItem(R.id.share_this_app).getActionProvider();

    /** Setting a share intent */
    mShareActionProvider.setShareIntent(getDefaultShareIntent());
    mShareActionProvider2.setShareIntent(getDefaultShareIntent2());

    return super.onCreateOptionsMenu(menu);

}

/** Returns a share intent */
private Intent getDefaultShareIntent(){
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");
    intent.putExtra(Intent.EXTRA_SUBJECT, "download the app");
    intent.putExtra(Intent.EXTRA_TEXT," play.google.com ");
    return intent;
}

private Intent getDefaultShareIntent2(){
    /*Your coude here*/
}
Tharkius
  • 2,744
  • 2
  • 20
  • 31