Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Copy to clipboard selected text from a TextView

Is there a possibility to copy to clipboard from a TextView UI component only the selected text?

I've catched the long press event and I copied the full text to clipboard, but now I want to specify the start and the end of the selection to be copied from a TextView.

Thank you.

like image 354
dorin Avatar asked Jul 08 '11 12:07

dorin


People also ask

How do I copy text from TextView?

Select + copy text in a TextView? Generally, we can copy / paste the value from EditText by long click. It is an in-build functionality for Android.

How do you copy text to clipboard on Android?

Highlight the text, long-press the selected text, then choose Copy. Long-press an empty field and select Paste to insert the copied text. Alternative method: Use the Gboard keyboard to manage the clipboard.

How do you create a clipboard on Android?

Sometimes you will see the clipboard icon right away in the menu alongside settings, GIF, and others. But if you don't see the icon, tap the three dots at the right to reveal the hidden icons. There you will see the clipboard icon.


2 Answers

TextView tv;
String stringYouExtracted = tv.getText().toString();
int startIndex = tv.getSelectionStart();
int endIndex = tv.getSelectionEnd();
stringYouExtracted = stringYouExtracted.subString(startIndex, endIndex);
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
clipboard.setText(stringYouExtracted);

EDIT (The previous is the full answer, but I ran into my answer by mistake so I would like to add):

With Newer APIs, change the last two lines to :

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

"Copied Text" is a title for your COPY entity in newer APIS

like image 127
Sherif elKhatib Avatar answered Oct 05 '22 02:10

Sherif elKhatib


you can do it this way:

ClipboardManager myClipboard = myClipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
ClipData myClip;
EditText editText = (EditText) findViewById(R.id.editText3);
int min = 0;
int max = editText.getText().length();
if (editText.isFocused()) {
    final int selStart = editText.getSelectionStart();
    final int selEnd = editText.getSelectionEnd();
    min = Math.max(0, Math.min(selStart, selEnd));
    max = Math.max(0, Math.max(selStart, selEnd));
}
// here is your selected text
final CharSequence selectedText = editText.getText().subSequence(min, max);
String text = selectedText.toString();


// copy to clipboard
myClip = ClipData.newPlainText("text", text);
myClipboard.setPrimaryClip(myClip);

Replace EditText with TextView

like image 20
SKG Avatar answered Oct 05 '22 02:10

SKG