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?

- 2,515
- 6
- 31
- 45

- 24,496
- 9
- 43
- 50
-
1ClipboardManager. http://developer.android.com/reference/android/content/ClipboardManager.html – IAdapter Oct 26 '08 at 17:21
-
This is basically a duplicate question of: http://stackoverflow.com/a/13517876/2116185 – user2116185 Apr 10 '13 at 13:19
15 Answers
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);
-
7
-
Does this function works on API 11+ (including lollipop) or not? – Ashraf Alshahawy May 25 '15 at 06:35
-
41
-
Is there a way to do this without the **Clip Tray** showing up? (LG G4S 5.1.1) – Alaa M. Dec 02 '16 at 22:52
-
2you can also add success message after the copying to tell the user that the text is copied `Context context = getApplicationContext(); Toast.makeText(context, "text copied", Toast.LENGTH_LONG).show();` – Shady Mohamed Sherif Jan 11 '17 at 13:01
-
1@AlaaM. I think this clip tray is option from the LG and you can't control it – Shady Mohamed Sherif Jan 11 '17 at 13:02
-
How to apply the unicode text? When the text contains emojis, it shows up like xF0x9Fx98x82 – satryaway Apr 24 '18 at 03:40
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" />

- 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
-
2Did you add target and min sdk to your manifest? If yes disable the lint warning. – Warpzit Oct 11 '12 at 21:02
-
4Add 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
-
4Can 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
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 oldandroid.text.ClipboardManager
. Same forClipData
. - 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.

- 202
- 3
- 11

- 484,302
- 314
- 1,365
- 1,393
-
2
-
@androiddeveloper, I've never actually used the label for anything, but see [this Q&A](http://stackoverflow.com/questions/33207809/what-exactly-is-label-parameter-in-clipdata-in-android). – Suragch Jan 17 '17 at 08:51
-
So even though it says it's visible to the users, it's actually not, and depends on developers to use it if needed. – android developer Jan 17 '17 at 08:53
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

- 757
- 1
- 8
- 23

- 1,680
- 19
- 22
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..

- 54,294
- 25
- 151
- 185

- 149
- 1
- 3
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());
}
}
}

- 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
-
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;

- 202
- 3
- 11

- 101
- 1
- 1
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.

- 18,405
- 4
- 60
- 77
@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;

- 2,321
- 2
- 29
- 44
-
-
@AlexS as I said, another friend already gave a correct answer, I'm just posting another way with more details, I think it would be useful for someone – Agna JirKon Rx Jan 24 '20 at 11:42
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)
}

- 31
- 1
-
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
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)

- 1,534
- 1
- 17
- 27
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())

- 992
- 1
- 11
- 24
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);
}
}

- 202
- 3
- 11
-
When yu shared a code, try to explain it. You can visit and check [how to answer a question](https://stackoverflow.com/help/how-to-answer). – Yunus Temurlenk Feb 25 '20 at 07:22
-
@YunusTemurlenk Thanks for your suggestions, next time I will edit my answers with a description – Mehul Boghra Feb 25 '20 at 07:31
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

- 1,481
- 11
- 7
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)
}

- 309
- 2
- 8