0

I'm building an android app and I need to direct the user to a URL which,after loading,asks the user to fill out a form and submit. Now from the android developer link I got this to read regarding WebViews:

"By default, a WebView provides no browser-like widgets, does not enable JavaScript and web page errors are ignored. If your goal is only to display some HTML as a part of your UI, this is probably fine; the user won't need to interact with the web page beyond reading it, and the web page won't need to interact with the user. If you actually want a full-blown web browser, then you probably want to invoke the Browser application with a URL Intent rather than show it with a WebView."

Does that mean I cannot use the web view in this case and I need to open the browser through intent ?

Anant Pathak
  • 167
  • 2
  • 5
  • 12
  • Try this. http://stackoverflow.com/a/30131309/4685284 – Shahbaz Hashmi Jul 17 '16 at 12:16
  • I want to know whether I need to open a browser through intent or should I open a webView ? – Anant Pathak Jul 17 '16 at 12:18
  • Use that code for your webview. It support javascript. Hence your submit and other client side events should work. – Shahbaz Hashmi Jul 17 '16 at 12:21
  • Okay I'll try...but what the italicized words from the android developer site imply is still not in sync with using webview for form submissions and user interactions. – Anant Pathak Jul 17 '16 at 12:22
  • 1
    Being a developer you cant assume anything before trying yourself. If webview doesn't support these features that u have mentioned. Then how we can fill up form and submit it in ucbrowser and other apps. They also using webview. – Shahbaz Hashmi Jul 17 '16 at 12:27
  • 1
    Don't forget to upvote that answer if it works. And definitely it will work :/ – Shahbaz Hashmi Jul 17 '16 at 12:43

2 Answers2

0

you can use use webview as a browser, if you web page open perfect in mobile browser, when you visit your website in mobile, webview also gives you same feeling.

there's some limitation in webview, but for your problem i think it will work perfect,but make sure your website supports mobile format. otherwise it will not look good.

0

You can use webview with form and intercept the post data as below:

  1. Create a html form

  2. Use Javascript and bind it to your android code to intercept the form data:

Enable Javascript :

WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);

Binding JavaScript code to Android code:

create javascript interface class:

public class WebAppInterface {
    Context mContext;

    /** Instantiate the interface and set the context */
    WebAppInterface(Context c) {
        mContext = c;
    }

    /** Show a toast from the web page */
    @JavascriptInterface
    public void showToast(String name , String email) {
        Toast.makeText(mContext, String.format("Name:%s - Email:%s",name,email), Toast.LENGTH_SHORT).show();
    }
}

You can bind this class to the JavaScript that runs in your WebView with addJavascriptInterface() and name the interface Android. For example:

WebView webView = (WebView) findViewById(R.id.webview);
webView.addJavascriptInterface(new WebAppInterface(this), "Android");

HTML and JavaScript that creates a toast message using the new interface when the user clicks submit button:

<script type="text/javascript">
   document.forms["frmSubmit"].onsubmit = function(){
     var name = document.getElementById("name").value;
     var email = document.getElementById("email").value;
     Android.showToast(name,email);
   }
</script>

References:

https://developer.android.com/guide/webapps/webview.html#UsingJavaScript

How to intercept POST data in an android webview

Android, Webview, get submitted form data

Android - how to intercept a form POST in android WebViewClient on API level 4

Community
  • 1
  • 1
Radi
  • 6,548
  • 18
  • 63
  • 91