A7madev
A7madev

Reputation: 728

How to add "Add to clipboard" in share intent

How can I add "Add to clipboard" icon in share intent?

I have this code

ClipboardManager copyToClipboard = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("Title", "Text");
copyToClipboard.setPrimaryClip(clip);

Intent clipboardIntent = new Intent();
clipboardIntent.putExtra(Intent.EXTRA_TEXT, "Extra Text");
clipboardIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");

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

Intent shareChooserIntent = Intent.createChooser(shareIntent, "Share via");
shareChooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { clipboardIntent });
startActivity(shareChooserIntent);

But still missing something, the icon doesnt show in the share intent

Upvotes: 3

Views: 4646

Answers (2)

KrzyShzy
KrzyShzy

Reputation: 156

To add an option in Share Via menu,
- First you need to create an activity to handle the data which will be shared through the Share Via menu.
- Second add that activity in the manifest file with the intent-filter for preferred mime-type.
- Third add shared-data handling code in the activity we created in step 1, in your case write code to add shared data to the clipboard'.
- Fourth Relax.

1. Activity Code

public class CopyToClipboardActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        finish();
        String receivedText;

        if (getIntent() != null && getIntent().getAction().equals(Intent.ACTION_SEND)) {
            receivedText = getIntent().getStringExtra(Intent.EXTRA_TEXT);
            Toast.makeText(this, recievedText, Toast.LENGTH_SHORT).show();

            //write shared-data handling code below to copy the received text to the clipboard. 

        }
    }
}

2. Manifest Code

<activity
    android:name=".CopyToClipboardActivity"
    android:theme="@android:style/Theme.Translucent.NoTitleBar">
    <intent-filter
        android:label="Copy to Clipboard">
        <action android:name="android.intent.action.SEND"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:mimeType="text/plain"/>
    </intent-filter>
</activity>

Now when you share some text from your app or any other app, you will see this 'Copy to Clipboard' option in the Share Via menu. And when you click on it, it will open the CopyToClipboardActivity but the activity is invisible as we have used the 'Theme.Translucent.NoTitleBar' theme in the manifest file. So you will not see it opening and closing. The activity will receive the text and now you can add that text to clipboard or do whatever you want to do with the received text in the activity.

Upvotes: 1

ataulm
ataulm

Reputation: 15334

You need a actual clipboard application installed which handles the ACTION_SEND intent for text/plain datatypes.

I have seen the icon you talk about show up in the chooser before, but I'm quite sure that it only showed up when I had a custom ROM installed; one that probably had a system application for this purpose. Can you give an example of an application for which the "Copy to Clipboard" option shows up on your device (without a clipboard application installed)?

Upvotes: 3

Related Questions