Adding an Easy Share Action in ActionBar

Vertumus picture Vertumus · Aug 19, 2013 · Viewed 10.3k times · Source

I have problem. I was follow a lot of guides, but still can't get my Share button to work. I got the icon showing in ActionBar, but when I press nothing is happening when android:showAsAction="always". But when android:showAsAction="never" it works. I just want share icon to be always shown in ActionBar. What I am doing wrong? Please help

Here is my code:

public class Main extends Activity {

    private ShareActionProvider mShareActionProvider;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        MenuItem item = menu.findItem(R.id.shareButton);
        mShareActionProvider = (ShareActionProvider) item.getActionProvider();
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.shareButton:
            Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND); 
            sharingIntent.setType("text/plain");
            String shareBody = "Check it out";
            sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Subject");
            sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
            startActivity(Intent.createChooser(sharingIntent, "Share via"));
            return true;
        case R.id.aboutButton:
            Intent intent = new Intent(this, About.class);
            startActivity(intent);
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
}

Here is my menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/shareButton"
        android:actionProviderClass="android.widget.ShareActionProvider"
        android:title="@string/share"
        android:showAsAction="always"/>  **---> when I put here "NEVER" then it works! But I want Share to be always as icon**

    <item
        android:id="@+id/aboutButton"
        android:actionProviderClass="android.widget.ShareActionProvider"
        android:showAsAction="never"
        android:title="@string/about_dev"/>
</menu>

Answer

Jonathan Hern&#225;ndez picture Jonathan Hernández · Apr 30, 2014

If you didn´t find a solution try this to get the ShareActionProvider

    @Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu, menu);
    mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.share)
            .getActionProvider();
    mShareActionProvider.setShareIntent(doShare());
    return true;
}

And doShare() would be:

    public Intent doShare() {
    // populate the share intent with data
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");
    intent.putExtra(Intent.EXTRA_TEXT, "Put whatever you want");
    return intent;
}