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.– Henqsan