Sayem
Sayem

Reputation: 86

webview loads webpage, but some contents get vanished after loading (Android Studio)

I tried to view http://artikelweb.com in a webview. The web page appears nicely. But, whenever I go to any author link from "Popular Authors" section, the web page appears, but, after loading, the quotes aren't showing.

In Google Chrome Browser(mobile) the quotes appear after loading,

Mobile Chrome View is here

but, in my app, the quotes are not showing in web view.

My app view is here

Code Snippet:

@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_web);

    myWebView = (WebView)findViewById(R.id.webView);
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    myWebView.loadUrl("http://www.artikelweb.com");
    myWebView.setWebViewClient(new WebViewClient());


}


@Override
public void onBackPressed() {
    if(myWebView.canGoBack()) {
        myWebView.goBack();
    } else {
        super.onBackPressed();
    }
}

Upvotes: 1

Views: 3948

Answers (2)

shadowsheep
shadowsheep

Reputation: 15002

In your particular case you must enable the DOM Storage API

webSettings.setDomStorageEnabled(true);

So your code must become:

@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_web);

    myWebView = (WebView)findViewById(R.id.webView);
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setDomStorageEnabled(true);
    myWebView.loadUrl("http://www.artikelweb.com");
    myWebView.setWebViewClient(new WebViewClient());
}

That becouse the javascript that use the website you are accessing needs it in its javascript.

enter image description here

enter image description here

Of course you could take advantage of a third party library, but again you won't know why this exact case works in the third party one and not in the default webview.

The library you are using has this initial settings:

https://raw.githubusercontent.com/delight-im/Android-AdvancedWebView/master/Source/library/src/main/java/im/delight/android/webview/AdvancedWebView.java

final WebSettings webSettings = getSettings();
        webSettings.setAllowFileAccess(false);
        setAllowAccessFromFileUrls(webSettings, false);
        webSettings.setBuiltInZoomControls(false);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        if (Build.VERSION.SDK_INT < 18) {
            webSettings.setRenderPriority(WebSettings.RenderPriority.HIGH);
        }
        webSettings.setDatabaseEnabled(true);
        if (Build.VERSION.SDK_INT < 19) {
            webSettings.setDatabasePath(databaseDir);
        }
        setMixedContentAllowed(webSettings, true);

        setThirdPartyCookiesEnabled(true);

Enabling by default many options that the default webview has disable for security reason.

Upvotes: 2

Upendra Shah
Upendra Shah

Reputation: 2301

Set UserAgent to your Webview settings and try

webSettings.setUserAgentString("Mozilla/5.0 (Linux; <Android Version>; <Build Tag etc.>) AppleWebKit/<WebKit Rev> (KHTML, like Gecko) Chrome/<Chrome Rev> Mobile Safari/<WebKit Rev>");

Upvotes: 0

Related Questions