Esir Kings Esir Kings -4 years ago 152
Android Question

ShareActionProvider Shows two Icons A System Icon And A share Icon

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.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download