1

I am trying to load a blog powered by Wordpress on Android WebView. While the content and the 'title' images are getting displayed, the images that are inside the posts (I mean opening a post from the displayed webpage) aren't getting displayed (all the text and even the ads are getting displayed). Viewing just the website on the web browser doesn't show such issues. I have tried the methods suggested in various other questions asked on this community regarding similar issues but failed to solve my problem.

Here is the code

public class MainActivity extends AppCompatActivity {

    private WebView webView;
    private ProgressDialog mProgress;

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

        webView = (WebView) findViewById(R.id.cuous_web_view);

        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setAllowContentAccess(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setAppCacheEnabled(true);
        webSettings.setAppCachePath(this.getApplicationContext().
                       getCacheDir().getAbsolutePath());
        webSettings.setLoadsImagesAutomatically(true);
        webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
        webSettings.setSupportZoom(true);
        webSettings.setUserAgentString("Android WebView");
        mixedContentIfRequired(webSettings);
        webSettings.setBuiltInZoomControls(true);

        webView.setWebViewClient(new CuousViewClient());
        webView.setWebChromeClient(new CuousChromeClient());
        webView.loadUrl("http://cuous.com/");
    }

    private void mixedContentIfRequired(WebSettings webSettings) {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
             webSettings.setMixedContentMode(WebSettings.
                        MIXED_CONTENT_COMPATIBILITY_MODE);
        }
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
            webView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        if(mProgress != null && mProgress.isShowing())
            mProgress.dismiss();
        super.onDestroy();
    }

    private class CuousChromeClient extends WebChromeClient  {

        @Override
        public void onProgressChanged(WebView view, int progress) {
            if (mProgress == null) {
                mProgress = new ProgressDialog(MainActivity.this);
                mProgress.show();
                mProgress.setCanceledOnTouchOutside(false);
            }
            mProgress.setMessage("Loading " + String.valueOf(progress) + "%");
            if (progress == 100) {
                mProgress.dismiss();
                mProgress = null;
            }
        }
    }

    private class CuousViewClient extends WebViewClient {

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            ActionBar actionBar = getSupportActionBar();
            if(actionBar.isShowing()) {
                actionBar.hide();
            }
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            return true;
        }


        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            view.loadUrl("javascript:var footer = document.getElementById(\"footer\"); footer.parentNode.removeChild(footer);");
        }

        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            if(handler != null) {
                handler.proceed();
            }
            else super.onReceivedSslError(view, null, error);
        }
    }
}
M J Learner
  • 171
  • 2
  • 11

1 Answers1

0

Hi I tried your link with mine code. I can see the images:see the photo. Is it this that you want?

Then is that mine mainAcitivity:

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;


public class MainActivity extends Activity {

    private WebView view;

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

        view = (WebView) this.findViewById(R.id.webView);
        view.getSettings().setJavaScriptEnabled(true);
        view.getSettings().setDomStorageEnabled(true);
        view.setWebViewClient(new MyBrowser());
        view.loadUrl("file:///android_asset/www/index.html"); //try js alert
        view.setWebChromeClient(new WebChromeClient()); // adding js alert support

    }
    private class MyBrowser extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.startsWith("tel:") || url.startsWith("sms:") || url.startsWith("smsto:") || url.startsWith("mailto:") || url.startsWith("mms:") || url.startsWith("mmsto:") || url.startsWith("market:")) {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(intent);
                return true;
            }
            else {
                view.loadUrl(url);
                return true;
            }
        }
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && view.canGoBack()) {
            view.goBack(); //method goback()
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }
}

Can also ask what is the minimum SDK version?

EDIT

This is what you want?

This is what also get

UPDATE_SHOULDURLOVERRIDING

This is a piece of code that will do something else then just open the url in webview it controls of it is start with something like mailto.... what I mean with mailto: In HTML you can do this: myname@gmail.com and on your pc it will open your outlook. If you have android and that piece of code isn't there it will give a error. If you have that piece of code it will ask in wich mailserivce do I need to open it.

EDIT So if you load it by this methode it works?

@Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

instead of:

        view.loadUrl("file:///android_asset/www/index.html"); //try js alert
Steven
  • 1,404
  • 2
  • 16
  • 39
  • Hi @Steven. Minimum SDK version is SDK 19 (Android 4.4 I think) onwards. As for the screenshot, I wasn't really referring to that. Perhaps I failed to explain it properly in the question though. Try opening the post which has "Health Kart Affiliates' mentioned. I was referring to the images that I should be seeing when I open that post. By the way could you also explain what those lines within the `if` statement in `shouldOverrideUrlLoading` mean? – M J Learner Jul 02 '17 at 07:40
  • Thank you very much for explaining that bit in `shouldOverrideUrlLoading` and as for the screenshots (edit), yes I was referring to that. You can see the embedded images in the post in the web browser but for some reason it doesn't show up in the webview. – M J Learner Jul 02 '17 at 08:06
  • Yes I see but some images in the post show up . I have taken a look from where the photo need to be downloaded to show them in your page. If the scr='' start with "https://i0.wp.com/...." it won't load. It's very strange – Steven Jul 02 '17 at 09:48
  • Do you own that website? – Steven Jul 02 '17 at 11:03
  • not really, I took this up as a 'learning project'. The domain/website is owned by someone known to me. By the way I happened to find some project online. Just editing the loadUrl line of that project works. Problem is that I can't find any additional line of code in it. Maybe this link should help? I'd be really grateful if you or anybody else could explain what the important difference is. [link](http://technoscripts.com/convert-website-into-android-app/) – M J Learner Jul 02 '17 at 11:09