Retrieving selected text in a WebView through the context action bar

It is known that it is difficult to get selected text in WebView, because WebView text selection is actually handled by the private WebTextView class .

However, with the recently released Android 4.0 Design Guidelines , there seems to be a glimmer of hope for this through context action panels (CABs). It says :

Use CAB when you allow the user to select data with a long press. You can control the contents of a CAB action to insert actions that the user would like to perform.

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

After the long click and the text selection mode begin, I can determine when the ActionModeoriginal copy / paste starts and changes Menu; however, I cannot figure out how to actually get the selected text.

+5
source share
3 answers

You cannot do this using the current API.

I requested this feature - Issue 24841: WebView should allow applications to create a custom contextual action bar http://code.google.com/p/android/issues/detail?id=24841

, WebView 4.0 (CAB). CAB WebView . , ActionMode, , , , getSelection() . .

+8

javascript : window.getSelection() WebView addJavascriptInterface .

0

thanks for your info, I solved a serious problem .. I just want to add some function to the actionmode. The following is my code, may be useful for 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;
}
0
source

All Articles