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() {
    valid.performClick();
    document.getElementById("ok").value = "start";
}
function refuseClick(){
    refuse.performClick();
    document.getElementById("no").value = "Je refuse";
}

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

</div>

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> 

MainActivity:

public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.about_screen);


        WebView  webview = (WebView) findViewById(R.id.webview);
        webview.loadUrl("file:///android_asset/index.html");

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

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

 }


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

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

Answers


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() {
    @JavaScriptInterface
    public void performClick() {
        Intent i = new Intent(this, CloudReco.class);
        startActivity(i);
    }
}, "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() {
    @Override
    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>
<html>
<body>
<a id='yourLinkID'></a>

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

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

</body>
</html>

Need Your Help

How do I display pdf in new tab?

c# asp.net asp.net-mvc 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: