I\'m making a webapp and I\'m using jQuery.
I\'ve made a simple android application with a WebView in it, and I load my url: www.mydomain.com
In mydomain.com
I'm no expert with Android native but we have just developed an app using jQuery based site within a webview. You need setJavaScriptEnabled(true) in your activity class and jQuery runs jsut fine. However your example with alert('Hi'); will not work because Android webviews don't support alerts by default. It can be enabled with a little googling... http://lexandera.com/2009/01/adding-alert-support-to-a-webview/
Alternatively we added a custom showAlert js function to display pretty user notifications.
Is JavaScript enabled in your webview...?
WebView.getSettings().setJavaScriptEnabled(true);
The problem seems to be the location of the jquery.js file. Post some code about how you are setting up the webView. That would give us some hints.
For an example, look at this: http://techdroid.kbeanie.com/2010/10/android-webview-javascript-and-css.html
Correct sequence-
webview.getSettings().setJavaScriptEnabled(true); webview.loadUrl("file:///android_asset/xxx.html");
By mistake dont put your invocation sequence go wrong like below
webview.loadUrl("file:///android_asset/xxx.html"); webview.getSettings().setJavaScriptEnabled(true);
Old question, but thought i'd add my 2cents anyway.
Javascript alerts do work in a webview, but you have to set the Web chrome client first.
webView.setWebChromeClient(new WebChromeClient())
Try this one: Create a Main Activity.
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
private WebView webView;
private JavaScriptInterFace javaScriptInterFace;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView=(WebView)findViewById(R.id.webView);
javaScriptInterFace=new JavaScriptInterFace(this);
webView.addJavascriptInterface(javaScriptInterFace, "AndroidFunction");
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("file:///android_asset/test.html");
webView.setWebViewClient(new WebViewClient());
}
}
Create another java file named JavaScriptInterFace
import android.content.Context;
import android.util.Log;
import android.widget.Toast;
public class JavaScriptInterFace {
Context mContext;
JavaScriptInterFace(Context c) {
mContext = c;
}
public int changeImage(){
Log.e("Got", "it"+2);
return 2;
}
public void showToast(){
Toast.makeText(mContext, "hi", Toast.LENGTH_SHORT).show();
}
}
the creat a html file put this one in project's asset folder
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width; user-scalable=0;" />
<title>My HTML</title>
</head>
<body>
<p id="mytext">Hello!</p>
<img alt="show" src="ic_left_arrow.png" id="myImage"/>
</br>
<input type="button" value="Change" onClick="changeImage()" />
<input type="button" value="Show tost" onClick="showToast()" />
<script language="javascript">
function changeImage() {
i=AndroidFunction.changeImage();
if(i===2){
document.getElementById('mytext').innerHTML = i;
document.getElementById('myImage').src="ic_right_arrow.png";
}
}
function showToast() {
AndroidFunction.showToast();
}
</script>
</body>
</html>
Put the required images in asset folder of your project along with the above html file.