262

I'm building an Android app and I want to copy the text value of an EditText widget. It's possible for the user to press Menu+A then Menu+C to copy the value, but how would I do this programmatically?

Ronak Thakkar
  • 2,515
  • 6
  • 31
  • 45
Zach
  • 24,496
  • 9
  • 43
  • 50

15 Answers15

486

Use ClipboardManager#setPrimaryClip method:

import android.content.ClipboardManager;

// ...

ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); 
ClipData clip = ClipData.newPlainText("label", "Text to copy");
clipboard.setPrimaryClip(clip);

ClipboardManager API reference

Edric
  • 24,639
  • 13
  • 81
  • 91
FlySwat
  • 172,459
  • 74
  • 246
  • 311
222

So everyone agree on how this should be done, but since no one want to give a complete solution, here goes:

int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.HONEYCOMB) {
    android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
    clipboard.setText("text to clip");
} else {
    android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); 
    android.content.ClipData clip = android.content.ClipData.newPlainText("text label","text to clip");
    clipboard.setPrimaryClip(clip);
}

I assume you have something like following declared in manifest:

<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="14" />
Warpzit
  • 27,966
  • 19
  • 103
  • 155
  • It doens't work, because the compiler throws an error: Call requires API level 11 (current min is 7): android.content.ClipboardManager#setPrimaryClip line 245 Android Lint Problem. – JavaRunner Oct 11 '12 at 20:09
  • 2
    Did you add target and min sdk to your manifest? If yes disable the lint warning. – Warpzit Oct 11 '12 at 21:02
  • 4
    Add following 3 lines above your method @SuppressLint({ "NewApi", "NewApi", "NewApi", "NewApi" }), @SuppressWarnings("deprecation"), @TargetApi(11).........That's what I did, and it works. – RRTW Jan 17 '13 at 05:13
  • 4
    Can anyone please tell me what exactly is the use of `label` in `newPlainText` method? the documentaion states `label User-visible label for the clip data.`. But when is the `label` visible to user ? And what kind of value/name should i put in `label` ? – shadyinside Jul 03 '14 at 16:02
  • @tannerjohn My guess is that it's some kind of preview that is used somewhere. But I don't know where. But you could just use the same text twice... – Warpzit Jul 03 '14 at 16:21
  • The easyest way to avoid "requires API level 11" errors, is to change the if(sdk <...) to the following: if (Build.VERSION.SDK_INT < 11) – Rudolf Polzer Jul 24 '15 at 07:19
  • Is that no way to call the default method of copy/paste/cut that already exsist in Android? –  Nov 30 '15 at 13:28
18

Android support library update

As of Android Oreo, the support library only goes down to API 14. Most newer apps probably also have a min API of 14, and thus don't need to worry about the issues with API 11 mentioned in some of the other answers. A lot of the code can be cleaned up. (But see my edit history if you are still supporting lower versions.)

Copy

ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("label", selectedText);
if (clipboard == null || clip == null) return;
clipboard.setPrimaryClip(clip);

Paste

I'm adding this code as a bonus, because copy/paste is usually done in pairs.

ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
try {
    CharSequence text = clipboard.getPrimaryClip().getItemAt(0).getText();
} catch (Exception e) {
    return;
}

Notes

  • Be sure to import the android.content.ClipboardManager version rather than the old android.text.ClipboardManager. Same for ClipData.
  • If you aren't in an activity you can get the service with context.getSystemService().
  • I used a try/catch block for getting the paste text because multiple things can be null. You can check each one if you find that way more readable.
Mehul Boghra
  • 202
  • 3
  • 11
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
17

Googling brings you to android.content.ClipboardManager and you could decide, as I did, that Clipboard is not available on API < 11, because the documentation page says "Since: API Level 11".

There are actually two classes, second one extending the first - android.text.ClipboardManager and android.content.ClipboardManager.

android.text.ClipboardManager is existing since API 1, but it works only with text content.

android.content.ClipboardManager is the preferred way to work with clipboard, but it's not available on API Level < 11 (Honeycomb).

To get any of them you need the following code:

ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);

But for API < 11 you have to import android.text.ClipboardManager and for API >= 11 android.content.ClipboardManager

null
  • 757
  • 1
  • 8
  • 23
Viachaslau Tysianchuk
  • 1,680
  • 19
  • 22
13
public void onClick (View v) 
{
    switch (v.getId())
    {
        case R.id.ButtonCopy:
            copyToClipBoard();
            break;
        case R.id.ButtonPaste:
            pasteFromClipBoard();
            break;
        default:
            Log.d(TAG, "OnClick: Unknown View Received!");
            break;
    }
}

// Copy EditCopy text to the ClipBoard
private void copyToClipBoard() 
{
    ClipboardManager clipMan = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
    clipMan.setPrimaryClip(editCopy.getText());
}

you can try this..

Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
ayrina
  • 149
  • 1
  • 3
8

Here is some code to implement some copy and paste functions from EditText (thanks to Warpzit for version check). You can hook these to your button's onclick event.

public void copy(View v) {      
    int startSelection = txtNotes.getSelectionStart();
    int endSelection = txtNotes.getSelectionEnd();      
    if ((txtNotes.getText() != null) && (endSelection > startSelection ))
    {
        String selectedText = txtNotes.getText().toString().substring(startSelection, endSelection);                
        int sdk = android.os.Build.VERSION.SDK_INT;
        if(sdk < android.os.Build.VERSION_CODES.HONEYCOMB) {
            android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
            clipboard.setText(selectedText);
        } else {
            android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); 
            android.content.ClipData clip = android.content.ClipData.newPlainText("WordKeeper",selectedText);
            clipboard.setPrimaryClip(clip);
        }
    }
}   

public void paste(View v) {
    int sdk = android.os.Build.VERSION.SDK_INT;
    if (sdk < android.os.Build.VERSION_CODES.HONEYCOMB) {
        android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        if (clipboard.getText() != null) {
            txtNotes.getText().insert(txtNotes.getSelectionStart(), clipboard.getText());
        }
    } else {
        android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        android.content.ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);
        if (item.getText() != null) {
            txtNotes.getText().insert(txtNotes.getSelectionStart(), item.getText());
        }
    }
}
live-love
  • 48,840
  • 22
  • 240
  • 204
  • Insn't it possible to call the copy/paste/cut methods of android without writing them like you are doing? I made a custom text-selection toolbar with the functionality for the user to be able to make a selected text bold/italic/underline etc. but after the customization, the default functions copy/paste/cut dont work anymore, but the icons for them still appears? –  Nov 30 '15 at 13:08
  • What's "WordKeeper" for? – android developer Jan 17 '17 at 08:30
7
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); 
ClipData clip = ClipData.newPlainText("label", "Text to copy");
if (clipboard == null || clip == null)
    return;
clipboard.setPrimaryClip(clip);

And import import android.content.ClipboardManager;

Mehul Boghra
  • 202
  • 3
  • 11
Mor2
  • 101
  • 1
  • 1
6

To enable the standard copy/paste for TextView, U can choose one of the following:

Change in layout file: add below property to your TextView

android:textIsSelectable="true"

In your Java class write this line two set the grammatically.

myTextView.setTextIsSelectable(true);

And long press on the TextView you can see copy/paste action bar.

King of Masses
  • 18,405
  • 4
  • 60
  • 77
3

@FlySwat already gave the correct answer, I am just sharing the complete answer:

Use ClipboardManager.setPrimaryClip (http://developer.android.com/reference/android/content/ClipboardManager.html) method:

ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); 
ClipData clip = ClipData.newPlainText("label", "Text to copy");
clipboard.setPrimaryClip(clip); 

Where label is a User-visible label for the clip data and text is the actual text in the clip. According to official docs.

It is important to use this import:

import android.content.ClipboardManager;
Agna JirKon Rx
  • 2,321
  • 2
  • 29
  • 44
3

For Kotlin, we can use the following method. You can paste this method inside an activity or fragment.

fun copyToClipBoard(context: Context, message: String) {

    val clipBoard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
    val clipData = ClipData.newPlainText("label",message)
    clipBoard.setPrimaryClip(clipData)

}
  • Thank you! The `context.` was the part I was missing - could be because I'm doing it within a fragment. – Cullub Apr 12 '20 at 21:05
3

For Kotlin use the below code inside the activity.

import android.content.ClipboardManager


 val clipBoard = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
 val clipData = ClipData.newPlainText("label","Message to be Copied")
 clipBoard.setPrimaryClip(clipData)
Rajeev Shetty
  • 1,534
  • 1
  • 17
  • 27
2

Or create a Kotlin extension

 fun String.copyToClipboard(context: Context) {
     val clipBoard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
     val clipData = ClipData.newPlainText("label",this)
     clipBoard.setPrimaryClip(clipData)
  }

and then call

"stringToCopy".copyToClipboard(requireContext())
Alex Busuioc
  • 992
  • 1
  • 11
  • 24
1

Here is my working code

/**
 * Method to code text in clip board
 *
 * @param context context
 * @param text    text what wan to copy in clipboard
 * @param label   label what want to copied
 */
public static void copyCodeInClipBoard(Context context, String text, String label) {
    if (context != null) {
        ClipboardManager clipboard = (ClipboardManager) context.getSystemService(CLIPBOARD_SERVICE);
        ClipData clip = ClipData.newPlainText(label, text);
        if (clipboard == null || clip == null)
            return;
        clipboard.setPrimaryClip(clip);

    }
}
Mehul Boghra
  • 202
  • 3
  • 11
0

Unless your app is the default input method editor (IME) or is the app that currently has focus, your app cannot access clipboard data on Android 10 or higher. https://developer.android.com/about/versions/10/privacy/changes#clipboard-data

Junsu Lee
  • 1,481
  • 11
  • 7
0

I use this(work with fragments)- kotlinish way

  private fun copyTextToClipboard(copyText: String) {

        val clipboardManager = requireActivity().
                               getSystemService(CLIPBOARD_SERVICE) as 
                                       android.content.ClipboardManager

        val clipData = ClipData.newPlainText("userLabel" ,copyText.trim())

        clipboardManager.setPrimaryClip(clipData)

    }
jafar_aml
  • 309
  • 2
  • 8