0

I have an Android app in which I'm giving the user a choice to Share the URL to the current page they're viewing. I'm implementing this code as follows:

public static void shareUrl(Context context, String url)
{
    Intent sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.putExtra(Intent.EXTRA_TEXT, url);
    sendIntent.setType("text/plain");
    context.startActivity(Intent.createChooser(sendIntent, "Share URL via..."));
}

When I call this method, I get a nice popup menu with a list of applications that can open 'text/plain' content, such as email clients and 'messages', see this screenshot.

I would like to add an option to this list, a simple option to copy the URL to the clipboard. Dropbox has such a feature, if you share any file in the public folder via the Dropbox app, you get this same list of app choices but in addition (right at the top) there is a 'Copy link' share option, which copies the link to your clipboard, see this screenshot.

I could just make a separate button on the ActionBar to copy the URL to the user's clipboard, but it fits very well in the Sharing menu in my opinion, and I don't want to waste that space on the ActionBar.

Is it possible to add a sharing option to this menu? How does Dropbox do this? I was thinking perhaps Dropbox has a separate app 'Copy link' that is simply there to receive URLs from Dropbox and copy them to the clipboard, but that makes no sense: (1) where is that app, I can't find it anywhere else on my phone? (2) why do I not get this app as an option when I share via my own app?

Nick Thissen
  • 1,802
  • 4
  • 27
  • 38

1 Answers1

-1

Try:

 ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); 
 clipboard.setText("Text to copy");

 Intent intent = new Intent();
 intent.setAction(Intent.ACTION_SEND);
 intent.putExtra(Intent.EXTRA_TEXT, "Extra Text"+clipboard.getText());
 intent.setType("text/plain");



 Intent chooserIntent = Intent.createChooser(intent, "Chooser Title");
 chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { clipboardIntent     });
 startActivity(chooserIntent);

as seen here the key is .putExtra().

The only one additional parameter for the chooser is Intent.EXTRA_INITIAL_INTENTS. Its description is:

A Parcelable[] of Intent or LabeledIntent objects as set with putExtra(String, Parcelable[]) of additional activities to place a the front of the list of choices, when shown to the user with a ACTION_CHOOSER.

as seen here

Community
  • 1
  • 1
Osman
  • 1,771
  • 4
  • 24
  • 47