Android - highlight a word/phrase in a WebView - android

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){}

Related

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 .

Highlighting and save the selected text from WebView?

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 .

Make paste option come up when long click text entry box in webview?

I am making an android app that uses a webview to visit a website. I am adding a feature, so that someone can press an option in the menu, and some text is added to the android clipboard.
Unfortunately, there would be no way to paste, as the dialog box on long click of a text entry box that usually appears isn't there by default.
How would I implement this? How would it be done?
Thank you for your time,
Liamwli
If your paste textbox is an Android textbox, you can add a code like this;
textbox.setOnLongClickListener(new onLongClickListener(){
//do whatever you want like displaying popup or paste the wanted text
});
If not, you can't do that because you can't use Android function as you want, but HTML function.
I hope that i understood and responded your question.
EDIT:
For webview, look around WebChromeClient. It enables to catch JS Alert.
So, you'll be able to throw event in JS with a specific library, and then catch it with Android. With this function, onJsAlert(WebView view, String url, String message, JsResult result) throw an alert on touch then catch it in Android.

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.

Selecting text on TextView (android 2.2)

How to implement selecting text capability on Android 2.2? I searched Google but can't find a solution.
This is the only way I've found (from google) to support it for android 1.6+, it works but it's not ideal, I think in stock android you can't hold down a webview to highlight until v2.3, but I may be mistaken..
By the way this is for a webview, it might also work on textview, but I haven't tried it
(Note: this is currently what my shipped app is using, so it works with all the phones I've had it tested on, but the only reason I found this question was because I was searching and hoping that someone had come up with a better way by now)
I've just got a menu item called "Select text" which calls the function "selectnCopy()"
#Override
public boolean onOptionsItemSelected(MenuItem item) {
//..
switch (item.getItemId()) {
case R.id.menu_select:
selectnCopy();
return true;
//..
}
}
Which looks like this:
public void selectnCopy() {
Toast.makeText(WebClass.this,getString(R.string.select_help),Toast.LENGTH_SHORT).show();
try {
KeyEvent shiftPressEvent = new KeyEvent(0, 0, KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_SHIFT_LEFT, 0, 0);
shiftPressEvent.dispatch(wv);
} catch (Exception e) {
throw new AssertionError(e);
}
}
Notice I've put the select_help string there as a toast, that's just because it's not immediately clear to the user how it's supposed to work
<string name="select_help">Touch and drag to copy text.</string>
The Link #Stefan Hållén provided only worked after API Level 11.
And Android 2.2 is API Lv.8, that's the reason why you cannot get a resource identifier.
Have you set the text to be selectable? like this:
android:textIsSelectable="false" //OR true
See the Documentation for further reference.
After a long and time consuming search, I can't find a component that can select text in textview for android API level <=11. I have written this component that may be of help to you :
new Selectable TextView in android 3 (API <=11) component
An interesting workaround:
You could try displaying your text in a webview.
You just have to write the HTML tags and all of that into your string to display, and it should be selectable using the WebKit browser.
This should be fairly lightweight and transparent to the user, and I think it would solve your problem.
Let me know if you need a code example, it should be fairly simple. Just check out the WebView docs on http://developer.android.com/resources/tutorials/views/hello-webview.html
Best of luck!

Categories

Resources