0

In my app I want copy the selected data to the clipboard without using the contextual actionbar that comes after long press on text in the webview.

buttonPlay.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {


                ClipboardManager mClipboard =
                        (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);

                KeyEvent shiftPressEvent = new KeyEvent(0, 0, KeyEvent.ACTION_DOWN,
                        KeyEvent.KEYCODE_SHIFT_LEFT, 0, 0);
                shiftPressEvent.dispatch(webView1);
                if(mClipboard!=null) {
                    text = mClipboard.getText().toString();
                    //Toast.makeText(MainActivity.this, "select_text_now     "+text, LENGTH_LONG).show();

                    //***************############################################28112013
                    //online part added

                    MyTask myTask = new MyTask();
                    myTask.execute(text);

                    //#############################################################

                    return;
                }
            }
        });

as you can see the text is fetched from the clipboard. I want to directly send the selected text to the clipboard on the press of this button. Please help, any suggestion is appreciated.

  • This example pretty much explains everything about [ClipboardManager](http://developer.android.com/reference/android/text/ClipboardManager.html) in android: [Copy to ClipBoard tutorial](http://www.tutorialspoint.com/android/android_clipboard.htm) – activesince93 Nov 23 '15 at 11:46
  • Hey Amitesh, did you find any solution? Let me know if you find it out. Thanks!!! – Ganesh Katikar May 12 '16 at 09:57
  • @GaneshKatikar no I didn't.. there was one solution that I found but it was pre gingerbread, so wasn't of much help... – Amitesh Banerjee May 16 '16 at 04:46
  • any one found solution? – Tara Jul 13 '18 at 04:22

1 Answers1

0

use this maybe it helps you

private void setClipboard(String text) {
        if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB) {
            android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
            clipboard.setText(text);
        } else {
            android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
            android.content.ClipData clip = android.content.ClipData.newPlainText("Text : ", text);
            clipboard.setPrimaryClip(clip);
        }
    }
Mustanser Iqbal
  • 5,017
  • 4
  • 18
  • 36
  • I know this method of setting text in the clipboard. what I'm asking is after I select a particular text portion after long pressing on the webview content, how will i send that text to the clipboard manager??? – Amitesh Banerjee Nov 26 '15 at 05:33