I have a Text Field and a Button. I'd make it so as soon as the button is pressed the text of the Text Field to be copied. How can this be done? As I search it (How to copy text programmatically in my Android app?), I read about a ClipboardManager method but rumours say it is also deprecated. Should I avoid it? Thanks
Asked
Active
Viewed 467 times
3 Answers
2
Honeycomb deprecated android.text.ClipboardManager
and introduced android.content.ClipboardManager
.
You should check whether android.os.Build.VERSION.SDK_INT
is at least android.os.Build.VERSION_CODES.HONEYCOMB
and therefore use one or the other.
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB)
{
// Old clibpoard
android.text.ClipboardManager clipboard = (android.text.ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
clipboard.setText("the text");
}
else
{
android.content.ClipboardManager clipboard = (android.content.ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
android.content.ClipData clipData = android.content.ClipData.newPlainText("PlainText", "the text");
clipboard.setPrimaryClip(clipData);
}

matiash
- 54,791
- 16
- 125
- 154
-
This answer is very much similar to my answer @matiash ..kindly check other answers too before posting new answers.. – Lal May 05 '14 at 15:49
-
-
Its ok @matiash ..I just pointed out..No need for sry and all..any way i'l upvote your answer too.. – Lal May 05 '14 at 15:54
1
Try the answer in this link..It says
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);
}
-1
Copy/Paste Offical Tutoiral Check it out
Relative Code :
ClipboardManager clipboard = (ClipboardManager)
getSystemService(Context.CLIPBOARD_SERVICE);
Copy the data to a new ClipData object:
For text
// Creates a new text clip to put on the clipboard
ClipData clip = ClipData.newPlainText("simple text","Hello, World!");