btate / BTAndroidWebViewSelection

Example user selection for android web views.
313 stars 126 forks source link

I've Seen a lot of people trying to get user selections with a context menu working in Android web views. The problem lies in Android's use of an intermediate text view that it places between the user and the web view on selection. So the javascript in the page doesn't know what's selected.

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.

The example uses an html page in the assets folder with the javascript methods included. The BTWebView class implements the interface methods necessary to draw the selection layer with the handles and show the context menu. Any content you wish to use this with will need the following javascript imports.

    <script src='jquery.js'></script>
    <script src='rangy-core.js'></script>
    <script src='rangy-serializer.js'></script>
    <script src='android.selection.js'></script>

This solution employs a few libraries that deserve recognition.

  1. The rangy javascript library by Tim Down.
  2. A wonderful drag and drop library by Bill Lahti.
    *Note: The version of this library included in this project has been slightly modified.
  3. The Quick Action library by Lorensius Londa.
    *Note: The version of this library included in this project has been slightly modified.

This project is freely available for use.