use this method:
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText(label, text);
clipboard.setPrimaryClip(clip);
at the place of setPrimaryClip we can also use the following methods:
void clearPrimaryClip()
Clears any current primary clip on the clipboard.
ClipData getPrimaryClip()
Returns the current primary clip on the clipboard.
ClipDescription getPrimaryClipDescription()
Returns a description of the current primary clip on the clipboard but not a copy of its data.
CharSequence getText()
This method is deprecated. Use getPrimaryClip() instead. This retrieves the primary clip and tries to coerce it to a string.
boolean hasPrimaryClip()
Returns true if there is currently a primary clip on the clipboard.
use [ClipboardManager][1]
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText(label, text);
clipboard.setPrimaryClip(clip);
make sure you have imported `android.content.ClipboardManager` and NOT `android.text.ClipboardManager`. Latter is deprecated.
Check this [link][2] for Further information.
[1]: http://developer.android.com/reference/android/content/ClipboardManager.html
[2]: http://developer.android.com/reference/android/content/ClipData.html#newPlainText%28java.lang.CharSequence,%20java.lang.CharSequence%29