jax
jax

Reputation: 38593

Open WebView form in the browser when pressing the submit button

I have an HTML form inside a WebView. The form control looks like a regular button, but actually it is an html file with a transparent background. When I press the HTML submit button, I want the result to open in the browser, not in the webview. Currently it is opening in the current WebView which is wrecking my layout.

How do I do this?

Here is my Form

<string name="googlecheckout_html_button">
<![CDATA[ 
<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>
<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\">
    <head>
    <title>Google Checkout Button</title>
    <script type=\"text/javascript\" language=\"javascript\">
        function pass() {
            return javaInterface.checkboxPass();
        }
    </script>
    </head>
    <body>
        <form style=\"width: 121px;margin-left: auto;margin-right: auto;\" onsubmit=\"return pass();\" action=\"https://%1$sapi/checkout/v2/checkoutForm/Merchant/%2$s\" id=\"BB_BuyButtonForm\" method=\"post\" name=\"BB_BuyButtonForm\">
            <input name=\"item_name_1\" type=\"hidden\" value=\"%3$s\" />
            <input name=\"item_description_1\" type=\"hidden\" value=\"%3$s\" />
            <input name=\"item_quantity_1\" type=\"hidden\" value=\"1\" />
            <input name=\"item_price_1\" type=\"hidden\" value=\"%4$s\" />
            <input name=\"item_currency_1\" type=\"hidden\" value=\"%5$s\" />
            <input name=\"_charset_\" type=\"hidden\" value=\"utf-8\" />
            <input type=\"hidden\" name=\"shopping-cart.items.item-1.merchant-private-item-data\" value=\"%6$s\" />
            <input alt=\"Pay With Google Checkout\" src=\"https://%1$sbuttons/buy.gif?merchant_id=%2$s&amp;w=121&amp;h=44&amp;style=trans&amp;variant=text&amp;loc=en_US\" type=\"image\" />
        </form>
    </body>
</html>
]]>
</string>

I am then using String.format(arg...) to populate the fields with appropriate values.

Here is by WebView

String form = String.format(getString(R.string.googlecheckout_html_button), 
                        host,
                        merchantId,
                        item_name_1,
                        item_price_1,
                        item_currency_1,
                        private_item_data
                        );

                if(Logging.DEBUG) Log.d(TAG, form);

                browser = new WebView(ActivityActivate.this);
                browser.setBackgroundColor(0);
                browser.getSettings().setJavaScriptEnabled(true);
                browser.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
                browser.getSettings().setSupportZoom(true);
                browser.setWebViewClient(new WebViewClient() {
                    @Override
                    public boolean shouldOverrideUrlLoading(WebView view,
                            String url) {
                        return false;
                    }
                });
                //browser.getSettings().setLoadsImagesAutomatically(true);

                browser.addJavascriptInterface(new JavascriptInterface(), "javaInterface");
                //browser.loadData(header+formData+footer, "text/html", "UTF-8");
                browser.loadDataWithBaseURL("https://checkout.google.com", form, "text/html", "UTF-8", null);
                llPaymentButtons.addView(browser);

Upvotes: 0

Views: 3262

Answers (1)

Peter Knego
Peter Knego

Reputation: 80340

Forms are submitted to servers not to browsers. Make sure you have a correct URL where you are submitting the form.

To solve your problem you must override WebViewClient.shouldOverrideUrlLoading(..):

webview.setWebViewClient( new WebViewClient() {
   public void shouldOverrideUrlLoading (WebView view, String url) {
       return false;
   }
});

Upvotes: 3

Related Questions