0

I trying to call a new Activity from my Webview. However, whenever i click on the specified <div> nothing happens. Please see my implementation below:

MainActivity.java

WebView myWebView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    myWebView = (WebView) findViewById(R.id.webView1);
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    myWebView.addJavascriptInterface(this, "Android");
    myWebView.loadUrl("http://.../hello.html");
}

@JavascriptInterface
public void newActivityCalled(){
        Intent intent = new Intent(this,NewAct.class);
        startActivity(intent);
}

hello.html

<!DOCTYPE HTML>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.css">
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.js"></script>
<script type="text/javascript">

    $("#yoyo").click(function (){
        Android.newActivityCalled();
    });

</script>
</head>
<body>
<div id="yoyo"> New Activity Text </div>
</body>
</html>
user3760741
  • 163
  • 1
  • 1
  • 10

1 Answers1

1

For some reason jQuery onclick method does not work. However, if you switch over to pure javascript then the code works:

<!DOCTYPE HTML>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.css">
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.js"></script>
<script type="text/javascript">
   function loadActivity(){
       Android.newActivityCalled();
  }
</script>
</head>
<body>
<div id="yoyo" onclick="loadActivity()"> New Activity Text </div>
</body>
</html>
user3760741
  • 163
  • 1
  • 1
  • 10
  • so any jQuery working ? or jquery all commands fail in android ? – Srinath Ganesh Jul 05 '14 at 17:07
  • @SrinathGanesh i haven't had a good experience using jQuery when calling methods in Android. E.g. i wanted to capture a url in webview and do somethin using shouldOverideUrlLoading() method but wasn't able to do so if the HTML was wrapped in jQuery. That too worked fine in pure HTML or javascript – user3760741 Jul 05 '14 at 17:47
  • am not much comfortable with UI stuff , but a jQuery hello world check will help seeing if anything is working – Srinath Ganesh Jul 06 '14 at 05:08