Akhilesh Sk
Akhilesh Sk

Reputation: 451

How to detect button click in WebView Android?

I was trying to detect the html button click of webview in java code.

My HTML Code:

<button id="fc_complete_order_button" class="fc_button" type="button" value="Complete Your Purchase" onclick="FC.checkout.validateAndSubmit()">Complete Your Purchase</button>
<div id="fc_complete_order_processing" style="display:none;">
    <strong class="fc_error"></strong> <br>
    <img src="//cdn.fox.com/static/v/1.1.0/images/ajax-loader.gif?ver=1" alt="Loading..." width="220" height="19">
</div>

My Java Code is:

final WebView browser = (WebView) findViewById(R.id.browser);

String html="";

try {
    Document doc1 = Jsoup.connect(lastLoadedURL).get();

    html=doc1.toString();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

browser.getSettings().setJavaScriptEnabled(true);

browser.addJavascriptInterface(new Object() {
    @JavascriptInterface
    public void validateAndSubmit() throws Exception {

        Toast.makeText(MainActivity.this, "Login clicked", Toast.LENGTH_LONG).show();
    }
}, "FC.checkout");

Can Anybody suggest where am doing wrong?

Upvotes: 2

Views: 2066

Answers (1)

Sergey Yamshchikov
Sergey Yamshchikov

Reputation: 1019

I assume "FC.checkout" is the problem. Google docs told us name the name used to expose the object in JavaScript . In JavaScript "FC.checkout" it's 2 objects: FC and checkout so you are trying to create checkout object inside FC object, but FC object doesn't exist in the browser and I suppose you can catch "object is undefined" exception.

I'm not sure and can't check it right now but I will check and update the answer.

Update: The assumption were correct. Just replace FC.checkout with FC (or anything else that meet JS naming rquirments) in both Java code and HTML

Upvotes: 1

Related Questions