WebView emulateShiftHeld() on Android Newer SDK's

I noticed older versions of the android platform used to have the emulateShiftHeld() function to select and copy text to the clipboard on the webview control.

i came up with this version of emulateShiftHeld():


    /**
     * Select Text in the webview and automatically sends the selected text to the clipboard
     */
    public void selectAndCopyText() {
        try {
         KeyEvent shiftPressEvent = new KeyEvent(0,0,KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_SHIFT_LEFT,0,0);
         shiftPressEvent.dispatch(mWebView);
        } catch (Exception e) {
            throw new AssertionError(e);
        }
    } 



Hope someone finds it useful.

6 comments:

MAD'u CrY said...

Hi,

It is working to select the text from web-view.And it displays coy to clip board.But i want to use that selected text for copy/paste/Replace options .
IS it possible to do all functionalities ?
Please suggest.
Thanks
Jetti.

Cosme Zamudio said...

as far as i know, its not possible.. as soon as you select something, the webview automatically sends everything to the clipboard and removes the selection... also, how do you plan to implement the replace and paste options? via javascript?

MAD'u CrY said...

Hi Zaudio,
As u said ,When the user selects the text,everything moves to clipboard. For paste option i want to store the selected text in variables. And use that variable when user selects paste option. And i want to do replace and paste using JavaScript only.
Any ideas to do....
Please give reply

Thanks
Jetti.

Solution said...

Hi,
I tried the code and try to retrieve it with the following with no luck.
Any suggestion? (using R2.2 Emulator)


ClipboardManager clipboard =
(ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
String s = (String) clipboard.getText();

nikhil.. said...

Can I get copyed starting index and ending index of text?

Martin said...

The shiftPressEvent code does not work on recent Android releases, in particular JellyBean, and probably not ICS.

Post a Comment

top