4

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:

enter image description here

Suhail Mehta
  • 5,514
  • 2
  • 23
  • 37

3 Answers3

0

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);
        }
    }
Community
  • 1
  • 1
hcpl
  • 17,382
  • 7
  • 72
  • 73
  • can u be more specific on selection event – Suhail Mehta May 24 '13 at 12:02
  • I've updated the question with some more detail. Apparently it's not as default as I thought it was. Looks like even the selection needs some custom code. Example added to my original answer. – hcpl May 24 '13 at 13:41
0

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.

laalto
  • 150,114
  • 66
  • 286
  • 303
0

If your using WebView Try to integrate Mozilla PDF.JS where you can render PDF . which can contain Images also .

Kantanand US
  • 108
  • 1
  • 1
  • 7