Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
how to select text in webview android with JavaScript?

I am using this code to highlight text in android webview.

    public void highlight(int pageNumber) {

    dbManager = new DBManager(webView.getContext());
    dbManager.open();

    cursor = dbManager.getSelectedID(books.get(0).getmDbId());
    String quotesDb = cursor.getString(cursor.getColumnIndex("_quote"));
    String currnetPage = cursor.getString(cursor.getColumnIndex("_current_page"));

    List<String> myList = Arrays.asList(quotesDb.split("½_½"));


    webView.evaluateJavascript("function doSearch(text, backgroundColor) {\n" +
            "    if (window.find && window.getSelection) {\n" +
            "        var windowHeight = window.scrollY;\n" +
            "        document.designMode = 'on';\n" +
            "        var sel = window.getSelection();\n" +
            "        sel.collapse(document.body, 0);\n" +
            "        while (window.find(text)) {\n" +
            "            document.execCommand('HiliteColor', false, backgroundColor);\n" +
            "            sel.collapseToEnd();\n" +
            "        }\n" +
            "        document.designMode = 'off';\n" +
            "        window.scrollTo(0, windowHeight);\n" +
            "    }\n" +
            "}", null);


    Log.e("SAVE_QUOTE ", "LIST_SIZE ----> " + myList.size());

        if (pageNumber == Integer.parseInt(currnetPage)) {
                for (int j = 0; j < myList.size(); j++) {

                    webView.evaluateJavascript("doSearch('" + myList.get(j) + "', 'SkyBlue')", null);
                 //   webView.findAllAsync(myList.get(j));
                }
        }
    }

Everything works fine until I select the code with a new line (paragraph). Then I get an error and the text is not highlighted.

/chromium: [INFO:CONSOLE(1)] "Uncaught SyntaxError: Invalid or unexpected token", source: file:///data/user/0/com.mms.tts/cache/W1/index.xhtml (1)
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!