3

I am adding a Share Action Using the Code Below

<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"
    tools:context="com.example.esir.jualeader.aspirant.MainActivity">
    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:title="@string/action_settings"
        app:showAsAction="never" />
    <item
        android:id="@+id/action_finish"
        android:orderInCategory="200"
        android:title="Exit"
        app:showAsAction="never" />
    <item
        android:id="@+id/share"
        android:title="Share"
        app:actionProviderClass="android.support.v7.widget.ShareActionProvider"
        app:showAsAction="ifRoom"
        />
</menu>

and

private ShareActionProvider mShareActionProvider;
private void setShareIntent(Intent shareIntent){

    if (mShareActionProvider != null) {
        mShareActionProvider.setShareIntent(shareIntent);
    }
}
private Intent createShareIntent(){
    Intent actionsend=new Intent();
    actionsend.setAction(Intent.ACTION_SEND);
    actionsend.putExtra(Intent.EXTRA_TEXT,"Please Download Jua Leader App From :  http://mstarsinnovations.com");
    actionsend.setType("text/plain");
    return Intent.createChooser(actionsend,"Share The Jua Leader Using");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main,menu);
    // Locate MenuItem with ShareActionProvider
    MenuItem item = menu.findItem(R.id.share);
    // Fetch and store ShareActionProvider
    mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
    setShareIntent(createShareIntent());
    // Return true to display menu
    return true;
}

The Result Is A Share Icon That Looks As Shown In The Image enter image description here

Why Is The Other Icon Appearing And It is the Only Clickable Icon.How Can I Remove It? Any Help Will be Highly Appreciated.

E_K
  • 2,159
  • 23
  • 39

2 Answers2

4

That's exactly what a ShareActionProvider is supposed to look like. If you just want a share button, then stop using ShareActionProvider. I.e., update your XML to remove the ShareActionProvider:

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

(You'll need to add your own @drawable/share to your app such as one from the material design icons).

Then override your onOptionsItemSelected() method to start your share when the menu item is tapped:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
  switch(item.getMenuId()) {
    case R.id.share:
      Intent shareIntent = createShareIntent();
      startActivity(shareIntent);
      return true;
    default:
      return super.onOptionsItemSelected(item);
  }
}

You don't need to do anything in onCreateOptionsMenu except inflating your menu.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • The [docs](https://developer.android.com/reference/android/support/v7/widget/ShareActionProvider.html#rankings) make it very clear: "The share action provider retains a ranking for each share target, based on how often the user chooses each one. The more often a user chooses a target, the higher its rank; **the most-commonly used target appears in the app bar as the default target.**" – ianhanniballake Feb 28 '17 at 03:34
  • Thanks For the Clarification – E_K Feb 28 '17 at 03:36
0

it shows that you have shared content with that app(icon shown right side to share icon) most recently.

KamDroid
  • 102
  • 4
  • 7