Dinesh
Dinesh

Reputation: 566

Android Text Selection In Webview

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.

Upvotes: 26

Views: 5115

Answers (2)

Priyank Patel
Priyank Patel

Reputation: 12382

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

Upvotes: -2

Surej
Surej

Reputation: 358

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

Upvotes: 8

Related Questions