Jega
Jega

Reputation: 696

How to enable the default highlight menus in android webview?

How to enable the default text highlights menu like: Copy/Paste/Search/Share in android webview ?

enter image description here

Upvotes: 21

Views: 3552

Answers (3)

Jorgesys
Jorgesys

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

RPB
RPB

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

hunter
hunter

Reputation: 35

Try this:

 mWebView.setHapticFeedbackEnabled(true);
 mWebView.setLongClickable(true);

Upvotes: -1

Related Questions