0

I am trying to implement the Share icon along with my overflow menu on the app. But the share icon is gray and I am unable to click on it.

My menu XML:

<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_action_share"
        android:actionProviderClass="android.widget.ShareActionProvider" />
    <item
        android:id="@+id/action_about"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="About"/>
    <item
        android:id="@+id/action_help"
        android:orderInCategory="200"
        android:showAsAction="never"
        android:title="Help"/>
    <item
        android:id="@+id/action_rate"
        android:orderInCategory="300"
        android:showAsAction="never"
        android:title="Rate"/>
</menu>

My menu Java code:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);

        MenuItem item = menu.findItem(R.id.menu_item_share);

        mShareActionProvider = (ShareActionProvider) item.getActionProvider();
        String strMsg = "Blood Pressure is: \t\t" + tvSVal.getText().toString() + "/" + tvDVal.getText().toString();

        Intent myIntent5 = new Intent();
        myIntent5.setAction(Intent.ACTION_SEND);
        myIntent5.putExtra(Intent.EXTRA_TEXT, strMsg);
        myIntent5.setType("text/plain");

        mShareActionProvider.setShareIntent(myIntent5);

        return true;
    }

    private void setShareIntent(Intent shareIntent) {
        if (mShareActionProvider != null) {
            mShareActionProvider.setShareIntent(shareIntent);
        }
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        case R.id.action_help:
            //display the help activity
            Intent myIntent2 = new Intent(getApplicationContext(), HelpFile.class);
            startActivityForResult(myIntent2, 0);
            overridePendingTransition(R.anim.right_slide_in, R.anim.right_slide_out);
            return true;
        case R.id.action_about:
            //display the about window
            Log.i("Opening About Activity", "ABOUT");
            Intent myIntent = new Intent(MainActivity.this, AboutApp.class);
            startActivityForResult(myIntent, 0);
            overridePendingTransition(R.anim.right_slide_in, R.anim.right_slide_out);
            return true;
        case R.id.action_rate:
            //Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(Intent.ACTION_VIEW);
            //Try Google play
            intent.setData(Uri.parse("market://details?id=com.test.testing"));
            if (MyStartActivity(intent) == false) {
                //Market (Google play) app seems not installed, let's try to open a web browser
                intent.setData(Uri.parse("https://play.google.com/store/apps/details?com.test.testing"));
                if (MyStartActivity(intent) == false) {
                    //Well if this also fails, we have run out of options, inform the user.
                    //let the user know nothing was successful
                }
            }
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
    private boolean MyStartActivity(Intent aIntent) {
        try
        {
            startActivity(aIntent);
            return true;
        }
        catch (ActivityNotFoundException e)
        {
            return false;
        }
    }

I get the following warning: The method setShareIntent(Intent) from the type MainActivity is never used locally

Please help me resolve it.

Si8
  • 9,141
  • 22
  • 109
  • 221

1 Answers1

1

You need to set the intent of the ShareActionProvider. When creating your menu, save an instance of the ShareActionProvider from the menu. Either at that point or later on, attach an intent to it via setShareIntent(...). For a more detailed explanation and an example, see the Google documentation here: http://developer.android.com/training/sharing/shareaction.html

Nathan Walters
  • 4,116
  • 4
  • 23
  • 37
  • You have to set the share intent outside of onOptionsItemSelected. Placing your code there means it will never be reached because you can't select that options item because it was never active in the first place! You should do it in `onCreateOptionsMenu(...)` to ensure that it is set when the menu is created. – Nathan Walters Jan 02 '14 at 17:45
  • I just updated my code to use the share intent within `onCreateOptionsMenu()` but it is still greyed out. I see the Share icon but I can't click on it. – Si8 Jan 02 '14 at 17:54
  • The warning is self explanatory: you never use that method. It's safe to delete, as it is basically a wrapper function for what you do inside `onCreateOptionsMenu(...)`. As for why it's still greyed out, the only explanation I can think of is that you don't have an app installed that can handle the MIME type "text/plain", which is unlikely. – Nathan Walters Jan 02 '14 at 18:14
  • I used it for another app and it seems to work fine but for this one it's just greyed out... Thinking if I need special permission in the manifest... – Si8 Jan 02 '14 at 18:19
  • Actually SILLY ME!!! I bet it was working the whole. I was testing the code in my emulator which doesn't have sharing enabled... DUMB THING!!! But I do have one question, why is the share icon and the overflow menu icon gray? I want it to be white with my ActionBar color. Any idea how to change it? – Si8 Jan 02 '14 at 18:24
  • 1
    You'll have to look in the resources in your SDK installation and copy the files into your own project. It's unfortunate, but that's just how android works. Go here `[SDK]/platforms/android-[VERSION]/data/res` and look for `ic_menu_share_holo_dark.png` in the appropriate resolution folders. – Nathan Walters Jan 02 '14 at 18:34
  • I fixed it through the style.xml :) Thanks. – Si8 Jan 02 '14 at 18:48