Pass Activity variable to javascript function in Android webview

Asked

Viewed 608 times

0

I’m trying to pass a variable that’s in Searchview on Toolbar from an Activity to a javascript function of a webview loaded html page, but I’m not getting it.

I have been researching for two days and all the examples I found have not worked. I am using sdkversion 16 on my test project and the devices on which the app will be directed are version 16 and higher.

Code from my webview:

    webView.setWebViewClient(new myWebClient());
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setBuiltInZoomControls(true);
    webView.getSettings().setDisplayZoomControls(false);
    webView.getSettings().setSupportZoom(true);
    webView.loadUrl("file:///android_asset/html/index.html");

Webclient:

public class myWebClient extends WebViewClient
{
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        // TODO Auto-generated method stub
        super.onPageStarted(view, url, favicon);
        progressBar.setVisibility(View.VISIBLE);
    }

    @Override
    public boolean  shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        super.shouldOverrideUrlLoading(view, request);
        progressBar.setVisibility(View.VISIBLE);
        return true;
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        // TODO Auto-generated method stub
        super.onPageFinished(view, url);
        progressBar.setVisibility(View.INVISIBLE);
    }

    @Override
    public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
        super.onReceivedError(view, request, error);
        webView.loadUrl("file:///android_asset/html/error.html");
    }
}

Searchview:

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.search, menu);

    MenuItem searchItem = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView)searchItem.getActionView();
    searchView.setOnQueryTextListener(this);
    searchView.setQueryHint(getString(R.string.search_hint));
    searchItem.setOnActionExpandListener(this);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    return super.onOptionsItemSelected(item);
}

@Override
public boolean onMenuItemActionExpand(MenuItem item) {
    return true;
}

@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
    return true;
}

@Override
public boolean onQueryTextSubmit(String query) {
    buscaString(query);
    return true;
}

@Override
public boolean onQueryTextChange(String newText) {
    return true;
}

public void buscaString(String s){
    //Solução utilizada
    webView.loadUrl("javascript:texto('"+s+"')");

}

Javascript function:

<head>
<script>
  function texto(val){
    var $context = $(".container");

    $context.removeHighlight();
    $context.highlight(val);    
}
</script>
</head>
  • I resolved the issue using webView.loadUrl("javascript:texto('"+s+"')");. I thank everyone who tried to help.

1 answer

0

First define a class with the annotation @JavascriptInterface in the methods.

public class JsHelper {
    private int attr1;
    private int attr2;

    @JavascriptInterface
    public int getAttr1() {
        return attr1;
    }

    @JavascriptInterface
    public void setAttr1(int attr1) {
        this.attr1 = attr1;
    }

    @JavascriptInterface
    public int getAttr2() {
        return attr2;
    }

    @JavascriptInterface
    public void setAttr2(int attr2) {
        this.attr2 = attr2;
    }
}

Create an instance of this class and move to Webview:

JsHelper helper = new JsHelper();
helper.setAttr1(2);
helper.setAttr2(0);

webView.addJavascriptInterface(javascript, "Android");

Within webView, you can access the attributes of this instance as follows:

console.log(Android.getAttr1());
  • Thanks for the help, but unfortunately it didn’t work for me.

  • Edit your answer there with what you have tried now. Maybe it is silly thing.

  • I modified the question with the changes. I also removed Alert because it did not work in the webview and modified the javascript function. But it still hasn’t worked.

  • I guess you forgot to webView.addJavascriptInterface(javascript, "Android");

  • I didn’t forget, I just changed the "javascript" contained in your exemlpo by helper, which is the object webView.addJavascriptInterface(helper, "Android");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.