0

I just want to call the following code of javascript on longclick on webview to disable copying/selection content of web view

var touching = null;

$('selector').each(function() {

    this.addEventListener("touchstart", function(e) {
        e.preventDefault();
        touching = window.setTimeout(longTouch, 500, true);
    }, false);

    this.addEventListener("touchend", function(e) {
        e.preventDefault();
        window.clearTimeout(touching);
    }, false);
});

function longTouch(e) {
    // do something!
}
  • Use javascript interface or simply call that function in webView.loadUrl(""); inside your onLongClick method of android – Saqib Apr 08 '14 at 05:38
  • I am new in android web apps . We can call methods of our javascript interface from javascripts code can we call javascipt method from our javascript interface? if yes then please give me sample code to do the same....it will be great help from your side..Thanks – user3509513 Apr 08 '14 at 05:48

1 Answers1

0

For that you have to define a class in java for the webInterface to interact with it, say

WebInterface.class

public class WebAppInterface {
    Context mContext;

    WebAppInterface(Context c) {
        mContext = c;
    }

    @JavascriptInterface
    public void yourMethod(String parameter) {
        //Do your stuff here
    }
}

you have to initialize this interface with your webview as below, considering you named your interface as 'interface' in your code

browser.addJavascriptInterface(new WebAppInterface(this), "interface");

then comes the web part, all you have to do is just define a function there and call your method from android interface and your app shall interact from your webview inside application

Your Web Code

<script type="text/javascript">
    function callYourMethod(parameter) {
        interface.yourMethod(parameter); //this value goes to android code
    }
</script>

For further query and understanding you can visit this link too javascript Interface Android

Saqib
  • 1,120
  • 5
  • 22
  • 40
  • The all stuffs i am doing to disable user selection in webview to prevent copy the content. And thanks for your answer. – user3509513 Apr 08 '14 at 06:09
  • then you should implement the long click for webview and do nothing inside that, or from properties disable long click over webview. – Saqib Apr 08 '14 at 06:10
  • I have done this nd its working fine but it will disable longclick listener we cannot perform any other task on longclick that's why I want to disable only user selection. – user3509513 Apr 08 '14 at 06:28
  • then check this answer http://stackoverflow.com/questions/5107651/android-disable-text-selection-in-a-webview – Saqib Apr 08 '14 at 06:33