Is it possible to select the text from a webview then to copy and paste. Is there any special method to do this??Please help me..
Asked
Active
Viewed 9,890 times
7
-
http://developer.android.com/reference/android/webkit/WebView.html#emulateShiftHeld%28%29 – Akram May 14 '12 at 13:01
-
Are you talking about allowing users to copy and paste in fields of a form being viewed in a Webview that you are displaying in your application? – Stan Kurdziel Oct 01 '12 at 20:44
-
@Stan not html form. normal texts in the html pages. – Faison N.P Oct 31 '12 at 07:24
-
I just tested on an embedded webview on gingerbread 2.3.7 and jelly bean 4.1.1. Nothing special in the code to allow copy/paste, but I was able to copy some text (from html body) and paste into another app or an html form in the same embedded webview. The copy/paste in webkit is a bit less smooth than in native android views, but after a couple long press attempts, it was possible. Maybe it has something to do with the html you are displaying in the WebView? – Stan Kurdziel Nov 01 '12 at 00:16
-
I need this feature on Android 2.1 devices. Sorry i forgot to add this on title. – Faison N.P Nov 07 '12 at 11:01
2 Answers
3
Hope this will help you...
public void selectAndCopyText() {
try {
Method m = WebView.class.getMethod("emulateShiftHeld", Boolean.TYPE);
m.invoke(BookView.mWebView, false);
} catch (Exception e) {
e.printStackTrace();
// fallback
KeyEvent shiftPressEvent = new KeyEvent(0,0,
KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_SHIFT_LEFT,0,0);
shiftPressEvent.dispatch(this);
}
}
override the touch events
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);
}
}

user755278
- 1,634
- 3
- 16
- 32
1
According to this site of someone's blog says " Copy functionality in a WebView is available by default in Android 3.0 and above", and may be this information may be help, Android: how to select texts from webview