0

I am trying to create a simple share menu which list down the various applications for sharing the text. I am following the same approach as android. This is my menuactivity

 public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.second, menu);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection.

        switch (item.getItemId()) {
            case R.id.read_aloud_menu_item:
                System.out.println("goes itno read aloud case");
                System.out.println(item.getItemId());
                mSpeech.speak(data, TextToSpeech.QUEUE_FLUSH, null);


                //mSpeech.speak("Hello how are you", TextToSpeech.QUEUE_FLUSH, null);
                mSpeech.setLanguage(Locale.US);
                System.out.println(item.getItemId());

                return true;
            case R.id.share:
                mShareActionProvider = (ShareActionProvider) item.getActionProvider();
                //sharejson(item.getItemId());
                Intent in=new Intent(Intent.ACTION_SEND);
                in.setAction(Intent.ACTION_SEND);
                in.setType("text/plain");
                in.putExtra(Intent.EXTRA_TEXT, "extra text that you want to put");
                mShareActionProvider.setShareIntent(in);
                //startActivity(Intent.createChooser(in, "Share via"));
                return true;
}
}

My xml

<?xml version="1.0" encoding="UTF-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:id="@+id/read_aloud_menu_item" 
         android:title="@string/read_aloud" 
         android:icon="@drawable/ic_reply_50" />
   <item android:id="@+id/share" 
         android:title="@string/share" 
         android:icon="@drawable/ic_reply_50" 
         android:showAsAction="ifRoom" 
         android:actionProviderClass="android.widget.ShareActionProvider" />
</menu>

The share option gets displayed in the list, but nothing happens on clicking the menu option. I also tried the createchooser but that too in vain. The createChooser gives an error "No applications can perform this action"

Chirag
  • 335
  • 2
  • 3
  • 13

2 Answers2

0

Use this

case R.id.share:
    Intent sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.putExtra(Intent.EXTRA_TEXT,
    "Application.");
    sendIntent.setType("text/plain");
    startActivity(sendIntent);
    return true;

In xml :

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >

<group android:id="@+id/group" >
    <item
        android:id="@+id/action_share"
        android:actionProviderClass="android.support.v7.widget.ShareActionProvider"
        android:icon="@drawable/share"
        android:title="Share"
        yourapp:showAsAction="ifRoom"/>
</group>

Ashwin S Ashok
  • 3,623
  • 2
  • 29
  • 36
0

Use this 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);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub

    switch (item.getItemId()) {
    case R.id.action_settings:
        Intent sendIntent = new Intent();
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.putExtra(Intent.EXTRA_TEXT, "Application.");
        sendIntent.setType("text/plain");
        startActivity(sendIntent);
        break;

    default:
        break;
    }
    return true;
}

and xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/action_settings"/>

</menu>

this worked for me

Meenal
  • 2,879
  • 5
  • 19
  • 43
  • 1
    ActivityNotFoundException again. I would like to mention here its not for android phone, the code i wrote is for google glass. So it might vary a bit. – Chirag Mar 18 '14 at 11:57
  • No. Basically what is happening is the list of applications should come out on clicking share. But instead with the code which i have mentioned in the question, it doesn't give the list of apps. Instead on using createchooser it gives a message saying "No applications can perform this action". I have facebook, twitter, g+ installed on Glass which appear in other applications' sharing options like sharing picture. – Chirag Mar 19 '14 at 07:52