8

I want to implement the cut,copy, paste functionality in my EditText,i tried with the following code :

    ClipMan = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
    editbox1.setText(ClipMan.getText());

for paste the text,but it paste the whole text in another Editbox..I want to copy the selected text and paste that text in the same Editbox just like the normal notepad works..

Any suggestions are greatly appreciated... Thanks !!

Nibha Jain
  • 7,742
  • 11
  • 47
  • 71
  • To copy and paste, [see this answer](http://stackoverflow.com/a/43131290/3681880). To cut, just copy first, and then delete the selection. – Suragch Mar 31 '17 at 02:20

3 Answers3

5

for copy data

ClipData clip = ClipData.newPlainText("simple text",Value);
ClipMan.setPrimaryClip(clip);

and get data which was copied

CharSequence pasteData="";
ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);
pasteData = item.getText();

in your case you can use EditText.getText() instead of Value.. and EditText.setText(pasteData); for paste that data.

Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
  • thanks CapDroid, i m using android 2.1 sdkVersion 7 and it is not resolving ClipData showing "ClipData cannot be resolved to a type" have any idea ?? – Nibha Jain May 31 '11 at 10:15
  • To bad that most examples have ClipData and that's available only from 3.0...! That's not usable, since most people use Froyo or prior, currently – User Apr 30 '12 at 14:04
  • this looks cool. Can u help me pasting the text inside a webview, where the text is copied before. Because in webview we cannot find id's for edit text.http://stackoverflow.com/questions/40377177/paste-text-in-edit-text-of-webview-in-android – Praveen Kumar Nov 03 '16 at 05:04
3

Finally i am able to copy ,paste in my application..now i can paste only selected text by using this code :

Editable s1;
EditText editbox2;

to copy the selected text :

       if(editbox2.getSelectionEnd() > editbox2.getSelectionStart()) 
        {
            s1 = (Editable) editbox2.getText().subSequence(editbox2.getSelectionStart(), editbox2.getSelectionEnd());
        }else
        {
            s1 = (Editable) editbox2.getText().subSequence(editbox2.getSelectionEnd(), editbox2.getSelectionStart());
        }

to paste the selected text:

             editbox2.getText().replace(Math.min(editbox2.getSelectionStart(),editbox2.getSelectionEnd()), Math.max(editbox2.getSelectionStart(), editbox2.getSelectionEnd()),s1, 0, s1.length());
Nibha Jain
  • 7,742
  • 11
  • 47
  • 71
0

we can do this way :

    int min2 = 0;
    int max2 = editText.getText().length();
    if (editText.isFocused()) {
        final int selStart = editText.getSelectionStart();
        final int selEnd = editText.getSelectionEnd();

        min2 = Math.max(0, Math.min(selStart, selEnd));
        max2 = Math.max(0, Math.max(selStart, selEnd));
     }
     ClipData abc = myClipboard.getPrimaryClip();
     ClipData.Item item = abc.getItemAt(0);

     String text = item.getText().toString();
     editText.getText().replace(min2,max2 , text);
SKG
  • 346
  • 4
  • 11