Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enabling general JavaScript in WebViewClient

While searching for an answer in google, it seems that I'm not the only one stuck with a problem that seems impossible to solve.

I've managed to create a WebView with a custom WebViewClient - this makes it possible for me to have a processdialog and show an error message if an URL couldn't be loaded.

But this creates a problem with JavaScript. The URL I'm loading has some JavaScript which changes some HTML elements CSS styles (showing or hiding element) or redirects to another location onclick - or maybe even want's to show an alert box. But by using the WebViewClient none of those are working.

This is how I load a page:

public void loadUrl(String url) {     final ProgressDialog dialog = ProgressDialog.show(myActivity.this, "",  getString(R.string.loading), true);             final WebView myWebView = (WebView) findViewById(R.id.webview);             myWebView.setVerticalScrollBarEnabled(false);             myWebView.setHorizontalScrollBarEnabled(false);             WebSettings webSettings = myWebView.getSettings();             webSettings.setJavaScriptEnabled(true);                                                        myWebView.setWebViewClient(new WebViewClient()              {                 @Override                 public boolean shouldOverrideUrlLoading(WebView view, String url)                  {                     Toast.makeText(myActivity.this, url, Toast.LENGTH_SHORT).show(); //Debugging purposes                 if (url.endsWith(".mp4"))                  {                         Intent intent = new Intent(Intent.ACTION_VIEW);                         intent.setDataAndType(Uri.parse(url), "video/mp4");                         view.getContext().startActivity(intent);                   }                 else                 {                                                    view.loadUrl(url);                     }                      return true;                 }                  public void onPageFinished(WebView view, String url)                  {                     //Toast.makeText(myActivity.this, "Oh no!", Toast.LENGTH_SHORT).show();                     dialog.dismiss();                 }                  public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)                  {                     Toast.makeText(myActivity.this, description, Toast.LENGTH_SHORT).show();                     String summary = "<html><body><strong>" + getString(R.string.lost_connection) + "</body></html>";                     myWebView.loadData(summary, "text/html", "utf-8");                    }                             }); //End WebViewClient              myWebView.loadUrl(url);              } 

This could probably be done in a smarter way, but I'm new at both Java and Android development...

Is it possible for me to enable the JavaScript for the WebViewClient at all? Removing the WebViewClient solves the problem, but then I can't catch events when the page errors or is finished loading.

like image 266
Repox Avatar asked Feb 23 '11 10:02

Repox


People also ask

How do I enable JavaScript on 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.

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.

How run js file in Android Studio?

After the library get downloaded , unzip the folder and copy the js. jar file and paste the jar to the libs folder inside the app folder in android project. Then right click the js. jar file and click “Add as Library”.


2 Answers

I don't know what your exact problem is, but i can enable the JavaScript and a custom WebViewclient without any problem:

WebView vistaWeb = (WebView) findViewById(R.id.webview); vistaWeb.setWebChromeClient(new MyCustomChromeClient(this)); vistaWeb.setWebViewClient(new MyCustomWebViewClient(this)); vistaWeb.clearCache(true); vistaWeb.clearHistory(); vistaWeb.getSettings().setJavaScriptEnabled(true); vistaWeb.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); 
like image 190
mdelolmo Avatar answered Sep 19 '22 02:09

mdelolmo


The proper way to enable JavaScript is by add the below two lines:

mWebView.getSettings().setJavaScriptEnabled(true); mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); 

Even if after adding its not working then try adding the below line.

mWebView.getSettings().setDomStorageEnabled(true); 

Now It should work. :)

like image 42
Tatson Baptista Avatar answered Sep 19 '22 02:09

Tatson Baptista