I have an app with a webview and I have been trying to get user-selected text from the body of the webview. You would think that the operating system would be able to handle something like the way iOS does, but this one of the many places where Android is utterly inadequate. I've probed this website and google and found nothing that could help me. My app, luckily has a javascript interface, to communicate with a script that we load into the webview. I thought i could use javascript to select the text from the webview. In chrome i can do this with the following bit of javascript:
window.getSelection.toString();
Right now i have a button which calls a function in my js file that will run the above command and return it's results to a method in my javascript interface. That message will toast the result of my javascript function. So i select text and then press this button. The only problem is that my toast message returns the following message:
javascript returned:
when it should return
javascript returned: <my selected text>
When i remove the '.toString(); part from my javascript command and select text and press a button i get teh following message
javascript returned: undefined
Why isn't this working?
Here's my code in context. This is hte javascript that gets called:
myNameSpace.getTextSelection = function()
{
var str;
if (window.getSelection){
str = window.getSelection().toString();
} else {
str = 'does not';
}
window.myJSHandler.getSelectedText(str);
};
and here's the java function it is calling
public void getSelectedText(String text)
{
String str = "function called. it returned: " + text;
Toast.makeText(getApplicationContext(), str, Toast.LENGTH_LONG).show();
}