1

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 }
    }
3
  • Can you clarify images not displaying and app not working? There are no images in the HTML you listed. Commented Mar 3, 2014 at 17:38
  • can you check edited code Commented Mar 3, 2014 at 17:50
  • I still can't tell exactly what you mean by the app is not working, but I've posted a few tips that should improve your program a bit. If you still have trouble, please post the exact details of the problem; "it doesn't work" is too vague! :-) Commented Mar 3, 2014 at 21:28

2 Answers 2

3

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".

Sign up to request clarification or add additional context in comments.

Comments

-1

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).show();  
            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).show();  
            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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.