i want allow user to select some texts from webview and it need to be send as a text message. pls find way to select text and copy to clipboard and extracting from clipboard. i saw many example but nothing helped me really...TIA
Edit
using the code provided in the link from @orangmoney52. with following changes
getmethod's second parameter and invoke method second parameter. if i give null there warning will come.. which one is correct?
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);
}
}
Getting this error:
05-26 16:41:01.121: WARN/System.err(1096): java.lang.NoSuchMethodException: emulateShiftHeld
The easiest way, although not as pretty as what seems like a per manufacturer implemented copy/paste feature, is the following:
https://bugzilla.wikimedia.org/show_bug.cgi?id=31484
Basically, if you're setting your own
WebChromeClient
viawebview.setWebChromeClient(...)
then text selection is disabled by default. To enable it yourWebChromeClient
needs to have to following method implemented:Step: 1 Create custom WebView class. This class will override the native action bar on long press on the webview text. Also it handles the the selection case for different version of android (tested on 4.0 onwards) This code takes the selected text using javascript.
}
Step 2: create separate class for WebView interface. This class listnes for event from once javascript code is getting executed
Step 3: Add menu.xml for custom menu in res > menu folder
I took help of several links listed below to achieve this: Thanks to you guys.
how to use javascript on webview http://developer.android.com/guide/webapps/webview.html#UsingJavaScript
for injecting javascript Why can't I inject this javascript in the webview on android?
for overriding default action bar How to override default text selection of android webview os 4.1+?
for version 4.0. to 4.3 text selection Webview text selection not clearing
@vnshetty, using the code provided in the link from @orangmoney52, I was able to complete this problem a few months ago. You can create a button in your menu that allows you to copy text. Then, in onOptionsItemSelected, you can have a clause like this:
The above answers looks perfectly fine and it seems you're missing something while selecting text. So you need to double check the code and find your overridden any TouchEvent of webview.
i Tried below code it works fine...
Function is
Call Above method wherever you want (You can put a button and call this method in its click event): emulateShiftHeld(mWebView);