How to copy selected text from textview to clipboard and paste it in another activity's edittext?

  • Replies:1
  • Answered
Vishnupriya Raju
  • Forum posts: 8

Feb 4, 2016, 6:19:32 AM via Website

   I am developing an android application. As a beginner i want your help to know how to copy selected text from textview to clipboard and paste it in another activity's edittext. I have tried with the following code to just copy it in clipboard. But the text is not copied. Can anyone please help me to solve this problem?

  Thanks in Advance.

public ClipData converttext()
{
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
fulltext = returnedText;
int min = 0;
int max = returnedText.getText().length();
if (returnedText.isFocused()) {
final int selStart = returnedText.getSelectionStart();
final int selEnd = returnedText.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 = returnedText.getText().subSequence(min, max);
String text1 = selectedText.toString();
myClip = ClipData.newPlainText("text", text1);
clipboard.setPrimaryClip(myClip);
return myClip;
}

Reply
Vishnupriya Raju
  • Forum posts: 8

Feb 4, 2016, 6:35:36 AM via Website

I had added an button and i gave the code under the onclick event. Now it is working fine.

Reply