Getting selected text in a WebView via a contextual action bar
Asked Answered
W

3

5

It's known to be difficult to get selected text in a WebView because WebView text selection is actually handled by a private class, WebTextView.

However, with the recently released Android 4.0 Design guidelines, there seems to be a glimmer of hope of achieving this through contextual action bars (CABs). It says:

Use CABs whenever you allow the user to select data via long press. You can control the action content of a CAB in order to insert the actions you would like the user to be able to perform.

Am I misinterpreting this? Is there a way to retrieve selected text from a WebView via a CAB?

After a long click and text selection mode begins, I can currently detect when the ActionMode starts and modify the original copy/paste Menu; however, I can't quite figure out how to actually retrieve the selected text.

Walkup answered 13/1, 2012 at 19:48 Comment(5)
Hey @octopi, could you please tell me how you actually detect when ActionMode starts and how you modified the menu? What I achieved is detecting a long click and start the ActionMode by myself, providing a custom bar through ActionMode.Callback, but that won't display the text selection cursors.Tabbitha
@davids. Without saying the selection, I also want to know how to defect or modified the menu. I have a quite ugly workaround if you do not need to use ActionMode other than text selection. I override the public ActionMode onWindowStartingActionMode (ActionMode.Callback callback) on the activity. Say return super.onWindowStartingActionMode(new MyActionModeCallback());. It work and the cursors do not disappear. However, I don't know what side-effect this workaround would cause. I can think of it is not system want us to do.Autonomic
@Autonomic i also have a need to override actionbar but need selection handleThurgau
@Octopi Can you please share how are you able to modify the original copy/paste menu.Sibyl
There was no way to do this through Android's APIs. We had to look at the Android Open Source Project to achieve the effect we wanted.Walkup
S
8

You can't do that yet with the current API.

I filed a feature request for this - Issue 24841: WebView should allow applications to supply a custom Contextual Action Bar http://code.google.com/p/android/issues/detail?id=24841

Basically, WebView in 4.0 has hardcoded its own Contextual Action Bar (CAB). That CAB has a reference back to the WebView and with that reference, it can get the selected text. I'm not sure how you were able to detect the ActionMode starting and modify the menu, but if you were able to do all of that, then you are stuck because getSelection() is package-private currently. I filed that as a separate issue and linked it to the previous issue above.

Size answered 27/1, 2012 at 22:53 Comment(1)
Still no fix for this?Amniocentesis
G
1

You can use javascript to get the selected text: window.getSelection(), and use WebView's addJavascriptInterface function to return the result.

Goner answered 6/2, 2012 at 15:1 Comment(1)
@johnnoodles Are you sure? I currently use something like the following to get selected text in a WebView: wvWebView.loadUrl("javascript:if0.textOut(document.getSelection().toString());"); where if0 is a JavascriptInterface with a method textOut(String s). It works fine for me (though I'm still having other problems) - does this break some part of the requested functionality that I've missed? Is the problem that this method is not strictly via the CAB?Overbearing
C
0

thanks for your information, I have solved a hard issue.. I just want to add some function into the actionmode. The following is my code, May be helpful to others.

@Override
public ActionMode onWindowStartingActionMode(Callback callback) {
    // TODO Auto-generated method stub
    ActionMode mode = super.onWindowStartingActionMode(callback);
    mode.getMenuInflater().inflate(R.menu.actions, mode.getMenu());
    mode.getMenu().findItem(R.id.action_add).setOnMenuItemClickListener(new OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            // TODO Auto-generated method stub
            Log.i("", "onMenuItemClick add ");
            return false;
        }
    });
    return mode;
}
Creon answered 4/3, 2014 at 14:10 Comment(1)
Wing Chen, answer is out of context. Please read actual question.Uniseptate

© 2022 - 2024 — McMap. All rights reserved.