6

Hi EveryOne I am trying to load GTV into Android WebView.It loads in Mobile browser quite well but not in webview. Here is my Code.

  WebView theWebPage  = new WebView(this);
    theWebPage.getSettings().setJavaScriptEnabled(true);

    theWebPage.getSettings().setPluginState(WebSettings.PluginState.ON);
    super.onCreate(savedInstanceState);
    getSupportActionBar().hide();
    setContentView(theWebPage);
    WebViewClient client = new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return false;
        }
    };
    theWebPage.setWebViewClient(client);

// client.onPageStarted(theWebPage);

    theWebPage.loadUrl("http://gtvqa.com/#!/");
2

4 Answers 4

17

I found this function helpful

this.webView.getSettings().setDomStorageEnabled(true);
Sign up to request clarification or add additional context in comments.

3 Comments

Welcome to SO! When posting code as solutions, it is helpful to explain how your code solves OP's problem :)
I had the same issue. I tried to wrap a web application based on AngularJs with webView in android studio. When I tried to log in nothing happened. After putting this line of code the application worked like magic within webView :)
This solved my issues.
8

AngularJS requires an HTML5-compliant browser and I don't think that the default Android WebView is. Unless you are the developer of that website and can add modernizer to the page to attempt to account for older, non-compliant browsers, you might not be able to solve this.

4 Comments

Ofcource I can add.By mordenizer do you mean modernizer.js ? I'll add and see.
I found another way to enable HTML5 in Enable HTML5 in WebView. By The way you identified the real problem.Thanks @mbielski
Yes, that is what I meant (modernizer.js) but glad you found a better solution to the problem.
I was looking for the reason why my angular 9 was not starting as expected. You saved me! Thanks!
1

By default android webview doesn't support HTML5 features, therefore sometimes there are issues with a AngularJS site.

You have to enable the HTML5 in webview to solve that issue.

Follow this method, you will be fine.

private void initWebView(View view) {
    webView=findViewById(R.id.webView);
    loadWebViewDatafinal(webView, webURL);
}

private void loadWebViewDatafinal(WebView wv, String url) {
        WebSettings ws=wv.getSettings();

        ws.setJavaScriptEnabled(true);
        ws.setAllowFileAccess(true);


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR) {
            try {
                Log.e("WEB_VIEW_JS", "Enabling HTML5-Features");
                Method m1=WebSettings.class.getMethod("setDomStorageEnabled", new Class[]{Boolean.TYPE});
                m1.invoke(ws, Boolean.TRUE);

                Method m2=WebSettings.class.getMethod("setDatabaseEnabled", new Class[]{Boolean.TYPE});
                m2.invoke(ws, Boolean.TRUE);

                Method m3=WebSettings.class.getMethod("setDatabasePath", new Class[]{String.class});
                m3.invoke(ws, "/data/data/" + getActivity().getPackageName() + "/databases/");

                Method m4=WebSettings.class.getMethod("setAppCacheMaxSize", new Class[]{Long.TYPE});
                m4.invoke(ws, 1024 * 1024 * 8);

                Method m5=WebSettings.class.getMethod("setAppCachePath", new Class[]{String.class});
                m5.invoke(ws, "/data/data/" + getActivity().getPackageName() + "/cache/");

                Method m6=WebSettings.class.getMethod("setAppCacheEnabled", new Class[]{Boolean.TYPE});
                m6.invoke(ws, Boolean.TRUE);

                Log.e("WEB_VIEW_JS", "Enabled HTML5-Features");
            } catch (NoSuchMethodException e) {
                Log.e("WEB_VIEW_JS", "Reflection fail", e);
            } catch (InvocationTargetException e) {
                Log.e("WEB_VIEW_JS", "Reflection fail", e);
            } catch (IllegalAccessException e) {
                Log.e("WEB_VIEW_JS", "Reflection fail", e);
            }
        }

        wv.loadUrl(url);
    }

Comments

-1

Just in case someone is wondering wants a c# answer,

webView.Settings.JavaScriptEnabled = true; 

This one line saved my life.

2 Comments

Add explanation to your code and format it correctly.
My android webview was unable to load an angular website. After trying a lot of unsuccessful fixes, adding this line to my initialized webview worked and the website loaded perfectly: webView.Settings.JavaScriptEnabled = true;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.