User
User

Reputation: 453

Passing data from java class to Web View html

I'm loading below html in my webView

https://mail-attachment.googleusercontent.com/attachment/?ui=2&ik=25c0c425c6&view=att&th=138db54ff27ad34b&attid=0.1&disp=inline&realattid=f_h5ahtmbe0&safe=1&zw&saduie=AG9B_P9YNooGjsk_jLefLptQ9q15&sadet=1343790299575&sads=-yBVsLKP_2mh7zMfYLCF7sL1u-w

Now what I want to do is to fill the textbox in the html that came from my java class variable and then automatically hit submit.

But I don't have any idea how to do this.

Any thougths will be appreciated.

Upvotes: 31

Views: 74896

Answers (6)

Javad Arjmandi
Javad Arjmandi

Reputation: 341

Solutions by Hungr would work, but using the same document they point out, I do the following:

in my Android code WebAppInterface class:

@JavascriptInterface
fun provideData(val input: String): String{
val output = ""
//some operation with input
    return output
}

then in host activity for webview:

webView.addJavascriptInterface(WebAppInterface(this), "Provider")

Inside your JS or HTML:

document.getElementbyId("Text").innerhtml = Provider.provideData(input);

Upvotes: 0

Denish Rana
Denish Rana

Reputation: 101

Just enable DOM Storage and write var x= to string:

webview.getSettings().setJavaScriptEnabled(true);
web.getSettings().setDomStorageEnabled(true);

webview.loadUrl(urlString);
webview.setWebViewClient(new WebViewClient(){

public void onPageFinished(WebView view, String url) {
    super.onPageFinished(view, url);

    String js = "javascript:var x =document.getElementById('username').value = '"+user+"';var y=document.getElementById('password').value='"+pass+"';";

    if (Build.VERSION.SDK_INT >= 19) {
        view.evaluateJavascript(js, new ValueCallback<String>() {
            @Override
            public void onReceiveValue(String s) {
            }
        });
    }
    else {
        view.loadUrl(js);
    }
}

Upvotes: 6

Levon Petrosyan
Levon Petrosyan

Reputation: 9625

Pass the paramter directly in the url

webView.loadUrl("file:///android_asset/animation.html?message=testing");

Get the paramter in html file

var url_string = window.location.href
var url = new URL(url_string);
var message= url.searchParams.get("message");

Upvotes: 1

xfdai
xfdai

Reputation: 2825

Be careful to call javascript function like this, the str may include single quote or other special characters.

String str = "xxx";
myWebView.loadUrl("javascript:xxx('"+str+"')");

I suggest to encode the str in base64, and decode it on javascript side.

  • Android

    String str = "xxx";
    //encode in base64
    String base64Str = Base64.encodeToString(str.getBytes(), Base64.NO_WRAP);
    myWebView.loadUrl("javascript:xxx('"+ base64Str +"')");
    
  • Javascript

    function xxx(val) {
        //decode from base64
        var str = atob(data)
    }
    

Upvotes: 4

hungr
hungr

Reputation: 2096

First, your URL seems not available.

If you want to do data exchange between android app and your web app/web page you can achieve this via javascript.

Here is an example from Android official site:

Create a class like this:

public class JavaScriptInterface {
    Context mContext;

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

    /** Show a toast from the web page */
    @JavascriptInterface
    public void showToast(String toast) {
        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
    }
}

In your WebView:

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

In your web page:

<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />

<script type="text/javascript">
    function showAndroidToast(toast) {
        Android.showToast(toast);
    }
</script>

If you wanna pass something to your webpage, just calling corresponding javascript function:

String str = "xxx";
myWebView.loadUrl("javascript:xxx('"+str+"')");

Here is the Reference: http://developer.android.com/guide/webapps/webview.html

Upvotes: 59

AlvaroSantisteban
AlvaroSantisteban

Reputation: 5336

I would add that the load of the javascript function should be done when the html is loaded. To control that, you can use the following:

webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("file:///android_asset/test.html");
webview.setWebViewClient(new WebViewClient(){
    public void onPageFinished(WebView view, String url){   
        webview.loadUrl("javascript:init('" + theArgumentYouWantToPass + "')");
    }           
});

test.html

<!DOCTYPE html>
<html>
<head>
    <title>Test</title>
</head>

<body>
hola
adios
</body>

<script type="text/javascript">

    function init(val){
// Do whatever you want with your parameter val
    }
</script>
</html>

Taken from Uncaught ReferenceError: myFunction is not defined at null:1 Android exception in webview

Upvotes: 30

Related Questions