Highlighting and save the selected text from WebView? - android

I am working on Android 4.0.3 . I am trying to select the text from the WebView and saving it as a string . I have tried this Code:
public void SelectText(){
try{
KeyEvent shiftPressEvent =
new KeyEvent(0, 0, KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_SHIFT_LEFT, 0, 0);
shiftPressEvent.dispatch(mWebView);
}catch(Exception e){
throw new AssertionError(e);
}
But it doesnot work . In Android 4 there is already an inbuilt menu for selecting text but how this menu could be overridden ??

This solution uses a javascript interface to pass touches to the page and effectively cut Android's native selection out of the equation. This has been tested from 2.2 to 4.0.3.
Look at this Github Project .

Related

android 4.x:Text Selection in webview

I am using android 4.x .As I select the data from webview , there
comes a option menu "copy" to copy the data on clipboard.
i wanna create own button .as i click on that button it will copy the
text on clipboard. i am able to copy selected text from webview to
clipboard till 2.3 version.
but it's not happening in android 3.0 or above.
It's measure problem.i have waste more time for it.Plz help me
My code is
public void SelectText(){
try{
KeyEvent shiftPressEvent = new KeyEvent(0, 0, KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_SHIFT_LEFT, 0, 0);
shiftPressEvent.dispatch(mainWebView);
}catch(Exception e){
throw new AssertionError(e);
}
}

Highlight Text feature like google play book

I want my android application to have text highlighting within a WebView. Similar to the functionality found in Google play book. Does any one have an idea how to achieve this?
I'm using a WebView because my content is in html form.
basically talking about this effect:
Starting from API level 11, you can use TextView's textIsSelectable flag.
Edit: Even though the question has now been edited to specifically mention WebView, OP #Suhail's comment "my content is in html form" does not fully disqualify TextView as it can also render some basic HTML.
The blue selection you see is part of the standard android environment when selecting text. So that should work on your standard webview without need of any custom code. => I'm no longer convinced this is true. Looks like it's not.
The green (yellow, orange, red, ...) selection however is custom.
You could read out the selected text from your selection event and use that information to update the html content, wrap the text in a span with a background color set.
Alternative approach is using javascript and enabling javascript in your webview. Not sure however how to get that done.
Some sources to check for that approach are https://github.com/btate/BTAndroidWebViewSelection and Android: How to select text from WebView, and highlight it onclick
Text selection from WebView details
To get the text selection working on a WebView you can use the following snippet (from this question). Trigger this on a button click (or other event) from your (context) menu.
private void emulateShiftHeld(WebView view)
{
try
{
KeyEvent shiftPressEvent = new KeyEvent(0, 0, KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_SHIFT_LEFT, 0, 0);
shiftPressEvent.dispatch(view);
Toast.makeText(this, "select_text_now", Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
Log.e("dd", "Exception in emulateShiftHeld()", e);
}
}
If your using WebView Try to integrate Mozilla PDF.JS where you can render PDF . which can contain Images also .

Android - How to start select mode

In my Android app I need to provide some custom actions on the contextual action menu after the user selects some text. To do so, I capture long clicks and start the action mode, providing a custom ActionMode.Callback (as pointed out in Android Docs). The problem is that this way the selection text cursors won't show up. So, is there any way to activate the selection text mode programmatically??
public void SelectText(){
try{
KeyEvent shiftPressEvent =
new KeyEvent(0, 0, KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_SHIFT_LEFT, 0, 0);
shiftPressEvent.dispatch(mWebView);
}catch(Exception e){
throw new AssertionError(e);
}
}
In this case we are activating text select on mWebView
Use startActionMode().
See How to invoke the ActionBar's ContextMenu-like behavior?

How to get the selected text in android webview

I need to get the selected text from webview and have to highlight the text permanently. I tried as selecting the text and while i am attempting to get the text using clipboard it showing me null pointer exception at "clipboard.getText()". I have tried code as
ClipboardManager mClipboard =
(ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
KeyEvent shiftPressEvent = new KeyEvent(0, 0, KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_SHIFT_LEFT, 0, 0);
shiftPressEvent.dispatch(webview);
if(mClipboard!=null)
{
String text = mClipboard.getText().toString();
Toast.makeText(this, "select_text_now "+text, Toast.LENGTH_LONG).show();
}
I need to get the selected text for highlight futher..please help me..
I have filed an Android bug for this: Issue 24842: WebView should make getSelection public. If you look at the ticket, you'll see some various workarounds.
Note that the code you posted is roughly similar to emulateShiftHeld which is deprecated in 4.0, which is probably why it is working in 2.2 and not in 4.0.

Android - highlight a word/phrase in a WebView

I want to select Text from Webview And Then Highlight only selected apparition of that word. someone has an idea? with javascript does not work because getSelection function return nothing on android webview.
Please help.
As far as I can understand, you can't select text on an Android touch device like you can on a desktop browser (with your mouse).
It seems like the way text is selected on android device only can be copied into the Clipboard or used by the native "Share" app.
Selecting the text => copy it into Clipboard => use the ClipboardManager to fetch it back.
I might is not as simple and smooth as you was hoping for, but maybe it will work out for you :)
Once you have the value of the selected text, your can always pass it to a javascript function that search/replace for it in your html and highlights it, by adding/padding some html to it.
Link to ClipboardManager.
http://developer.android.com/reference/android/text/ClipboardManager.html
Here's a demo of fetching selected text with your mouse on a regular desktop browser:
http://bit.ly/AwggX9
Try running the same with your Android device. The text selection doesn't work the same way. I guess its the Share/Clipboard manager that handles the selection, and not the browser itself.
Good luck, hope you find a solution that works, and will be happy to hear about it.
Have you looked into:
mainpage.showFindDialog("Alice", true);
where mainpage is the WebView...
This will popup above the webview a bar where the text is entered - and then highlight the first occurrence of the text. There are arrows to jump to the next, etc...
Unless you mean you wish to highlight certain words - and have them stay that way - then you need to inject some javascript to do a highlighting function.
The following code will copy your text to clipboard then from clipboard fetch the text and search it..
private void emulateShiftHeld(WebView view)
{
try
{
KeyEvent shiftPressEvent = new KeyEvent(0, 0, KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_SHIFT_LEFT, 0, 0);
shiftPressEvent.dispatch(view);
}
catch (Exception e)
{
Log.e("dd", "Exception in emulateShiftHeld()", e);
}
}
The following code will highlight you the text you have selected...
ClipboardManager ClipMan = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipMan.setText(ClipMan.getText().toString());
wb.findAll(ClipMan.getText().toString());
try
{
Method m = WebView.class.getMethod("setFindIsUp", Boolean.TYPE);
m.invoke(wb, true);
}
catch (Throwable ignored){}

Categories

Resources