26

I am using webview for displaying content in Android Honeycomb(3.x). I created customized action menu for cut,copy and paste.How can i copy the selected text in Webview by using my customized action menu.

Srivathsan
  • 519
  • 4
  • 18
Dinesh
  • 566
  • 4
  • 16

2 Answers2

8

May It Will Help...

public void selectAndCopyText() {
try {
    Method m = WebView.class.getMethod("emulateShiftHeld", null);
    m.invoke(this, null);
} catch (Exception e) {
    e.printStackTrace();
    // fallback
    KeyEvent shiftPressEvent = new KeyEvent(0,0,
         KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_SHIFT_LEFT,0,0);
    shiftPressEvent.dispatch(this);
}

}

Got from https://stackoverflow.com/a/1113204/638987

Community
  • 1
  • 1
Surej
  • 358
  • 1
  • 2
  • 16
  • It works on Android 3.2! Some others answers suggested boolean parameter in emulateShiftHeld --- it is not correct! Use "null" as in code above and it will work for you! – Mike Keskinov Sep 21 '12 at 15:07
  • Under Anroid 4.0.3 m.invoke(webView, null) cause native crash `09-21 23:20:49.151: A/libc(4103): Fatal signal 11 (SIGSEGV) at 0x00000014 (code=1)`. – Mike Keskinov Sep 21 '12 at 15:20
-2

Try below code...

private void emulateShiftHeld(WebView view)
{
    try
    {
        KeyEvent shiftPressEvent = new KeyEvent(0, 0, KeyEvent.ACTION_DOWN,
                                                KeyEvent.KEYCODE_SHIFT_LEFT, 0, 0);
        shiftPressEvent.dispatch(view);
        Toast.makeText(this, "select_text_now", Toast.LENGTH_SHORT).show();
    }
    catch (Exception e)
    {
        Log.e("dd", "Exception in emulateShiftHeld()", e);
    }
}

and Call above method wherever you want...

emulateShiftHeld(mWebView);

for more details see this... Android: how to select texts from webview

Community
  • 1
  • 1
Priyank Patel
  • 12,244
  • 8
  • 65
  • 85
  • This is code for Android 2.2. Surej published code which works for both Android 2.2 & 3.2, but this is still problem for Android 4.* (Ice Cream Sandwich). The code above just do nothing under Ice Cream Sandwich. – Mike Keskinov Sep 27 '12 at 16:34