Android HTML button onclick event

I have images displaying in Local HTML pages and have one button for click event. When click this button i need to call another activity. It's working. But the issue is after going another then come back to HTML pages the images are not displaying and app is not working.

<script language="javascript">
function validClick() {
    document.getElementById("ok").value = "start";
function refuseClick(){
    document.getElementById("no").value = "Je refuse";

<button type="button" id="ok"
    style="font-weight: 700; margin-right: 20px;" onclick="validClick();">Start</button>


HTML page contain image:

<li><a class="box" href="products.html" data-transition="fade" > <img src="img/products.png" alt="Products"> <span class="text"> Products</span> 


public void onCreate(Bundle savedInstanceState)

        WebView  webview = (WebView) findViewById(;

        valid = new Button(this);
        refuse = new Button(this);

     // Enablejavascript
        WebSettings ws = webview.getSettings();
        // Add the interface to record javascript events
        webview.addJavascriptInterface(valid, "valid");
        webview.addJavascriptInterface(refuse, "refuse");


    public void onClick(View v) {
        if (v.equals(valid)) {

        Intent i = new Intent(this, CloudReco.class);
        } else if (v.equals(refuse)) {
            //do Something else }


There are a few things you need to check with your code.

First, in your HTML, the <script> tag at the top is not closed. Please add a </script> after your last function definition.

Second, in the Java, please configure your WebView before calling loadUrl. Settings and JavaScript interfaces should be registered first.

Third, there's no need to use a Button widget to achieve the effect you are looking for. Something like this should be perfectly adequate:

webview.addJavaScriptInterface(new Object() {
    public void performClick() {
        Intent i = new Intent(this, CloudReco.class);
}, "valid");

and similar for "refuse".

The next "strange" method I've used to pass a lot of javascript event (last use was fro scroll event and I was passing also the value concatenated):

webView.setWebViewClient(new WebViewClient() {
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.contains("button1Event")) {
            // button1 was clicked inside webview html
            Toast.makeText(context, 'Button1WasClicked', Toast.LENGTH_SHORT);  
            return true; (to avoid a default redirect to the url)
         } else if (url.contains("button2Event")) {
            // button2 was clicked inside webview html
            Toast.makeText(context, 'Button2WasClicked', Toast.LENGTH_SHORT);  
            return true; (to avoid a default redirect to the url)
         return false;

HTML contains a dummy <a> element used to simulate the URLclick where URL is a KEY that we are waiting in Native code:

<!DOCTYPE html>
<a id='yourLinkID'></a>

<button onclick="myFunction('button1Event')">Button1</button>
<button onclick="myFunction('button2Event')">Button2</button>

function myFunction(value) {
    document.getElementById('yourLinkID').href = 'testtest' + value; 
// we set the wanted URL and simulate the click


Need Your Help

How do I display pdf in new tab?

c# pdf

I would like to add data to a pdf from my controller then display in a new tab.

PHP, Regex replace line breaks in user input only between two reserved charcters

php regex preg-replace

I am dealing with a textarea user input which some characters are reserved for example: