Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript not working in Android Webview?

I'm trying to make an Android version of a relativly simple iOS app that uses a webview, some buttons and then relies on javascript calls to a CMS.

But I'm stuck at a pretty early point of development: The webview doesn't function with javascript.I've read a lot of posts about how to enable JS in an Android webview, but no luck so far.

Below is some of my code:

public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.main);      mWebView = (WebView) findViewById(R.id.webview);     mWebView.getSettings().setJavaScriptEnabled(true);     mWebView.setWebChromeClient(new WebChromeClient());     mWebView.setWebViewClient(new HelloWebViewClient()     {         @Override           public void onPageFinished(WebView view, String url)           {               //Calling an init method that tells the website, we're ready              mWebView.loadUrl("javascript:m2Init()");             page1(mWebView);         }       });   mWebView.loadUrl("http://my_url/mobile/iphone//app.php");   }  private class HelloWebViewClient extends WebViewClient {     @Override     public boolean shouldOverrideUrlLoading(WebView view, String url) {         view.loadUrl(url);         return true;     }  }   @Override public boolean onKeyDown(int keyCode, KeyEvent event) {     if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {         mWebView.goBack();         return true;     }     return super.onKeyDown(keyCode, event); }  public void page11(View view) {     mWebView.loadUrl("javascript:m2LoadPage(1)"); } 

What am I doing wrong here? The URL is working perfectly in my iOS app, and in a browser. But not in my app!

Please tell me it's something obvious...

like image 524
David K Avatar asked Sep 25 '11 19:09

David K


People also ask

How do I enable JavaScript on Android WebView?

JavaScript is disabled in a WebView by default. You can enable it through the WebSettings attached to your WebView . You can retrieve WebSettings with getSettings() , then enable JavaScript with setJavaScriptEnabled() . WebView myWebView = (WebView) findViewById(R.

Why is my WebView not working?

You might often face issues in updating the chrome and Android System Webview. To fix this problem, you can reboot your device, check your internet connection, stop auto-updating all apps, clear Google Playstore cache, and storage, leave the beta testing program, and manually update Android WebView app from Playstore.

Can we use JavaScript in Android Studio?

Can we use JavaScript for Android? Yes, of course! The Android ecosystem supports the concept of hybrid apps, which is a wrapper over the native platform. It mimics the UI, UX, and all kinds of hardware and network interactions, just like how you would use a native Android app.

Is Android WebView deprecated?

This interface was deprecated in API level 12. This interface is now obsolete.


1 Answers

FIXED! Spurred on by the error, I found out that I needed to set

setDomStorageEnabled(true)

for the webview settings.

Thanks for your help Stephan :)

like image 76
David K Avatar answered Oct 05 '22 16:10

David K