Reputation: 696
How to enable the default text highlights menu like: Copy/Paste/Search/Share in android webview ?
Upvotes: 21
Views: 3552
Reputation: 126465
Working on Android 1.5 - 2.3 you can use emulateShiftHeld()
made public since 2.2 but now is deprecated. this method put your WebView into text selection mode.
https://developer.android.com/reference/android/webkit/WebView.html#emulateShiftHeld%28%29
Unfortunately there's no copy/paste/search/share function integrated in Android, since Android 2.0 the text selection can be driven by touch but other than that, there's no other thing you can do.
Upvotes: 4
Reputation: 16330
I found a workaround for this Check out method selectText() on WebView (it's not in API, but can be invoked using reflection)
here is my full method source code:
public void startTextSelection() {
try {
WebView.class.getMethod("selectText").invoke(this);
} catch (Exception e) {
try {
WebView.class.getMethod("emulateShiftHeld").invoke(this);
} catch (Exception e1) {
KeyEvent shiftPressEvent = new KeyEvent(0, 0,
KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_SHIFT_LEFT, 0, 0);
shiftPressEvent.dispatch(this);
Toast.makeText(getContext(), R.string.select_text, Toast.LENGTH_LONG).show();
}
}
}
Works on ICS too.
Upvotes: 2
Reputation: 35
Try this:
mWebView.setHapticFeedbackEnabled(true);
mWebView.setLongClickable(true);
Upvotes: -1