Java – Android webview loadurl is slow

Android webview loadurl is slow… here is a solution to the problem.

Android webview loadurl is slow

I’m working on an application and I’m loading a web page from an external URL in a web view.
Loading pages takes a long time to load, taking 30 seconds to 1 minute
So please see my code here

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

public class WebActivity extends AppCompatActivity {

private WebView webView;
    private ProgressBar progressBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        webView = (WebView)findViewById(R.id.webView);
        progressBar= (ProgressBar)findViewById(R.id.progressBar2);
        String link = getIntent().getExtras().getString("webLink");
        String title = getIntent().getExtras().getString("webTitle");
        setTitle(title);
        webView.setVisibility(View.GONE);
        progressBar.setVisibility(View.VISIBLE);
        Log.d("WEB", link);
        webView.setWebViewClient(new MyBrowser());
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setUseWideViewPort(true);
        webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setAppCachePath(String.valueOf(getCacheDir()));
        webView.getSettings().setAppCacheEnabled(true);
        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.loadUrl(link);
    }

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android. R.id.home:
                 app icon in action bar clicked; goto parent activity.
                this.finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

public class MyBrowser extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return super.shouldOverrideUrlLoading(view, url);
        }

@Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            webView.setVisibility(View.VISIBLE);
            progressBar.setVisibility(View.GONE);
        }
    }

}

Any ideas for improving performance?

Solution

Unfortunately, you can’t fix this. And yet:

  • Try loading your URL in your Android device’s browser. Is it faster? Otherwise, there’s really nothing you can do.

There are a few things you can try, though, and a few things to check. Specifically:

  • You set the visibility to View.GONE (making your webview invisible) when the page loads, and then make it visible again after the page loads. This may be the problem.

    Try not to use it and you may find that it will be faster. In my experience, onPageFinished(..) only fires for some time after the page loads.

  • Do pages really need JavaScript? If not, do not enable it.

  • If your situation works, you can use an HTML parser like jsoup. Extract only the data you need from the page and display it to the user. This will be much faster.

    If a page uses Ajax to load data dynamically, you can also load data directly from the endpoint it uses. Open the page in a desktop browser and open the web tab of the developer tools to see how the page works and loads the data.

  • You can use shouldInterceptRequest(..) to block requests from WebView. This might help if the page has something similar. Facebook shares buttons or extra images you don’t need. Blocking these will speed up loading times.

If you show us the URL you are using, maybe I can do more investigation and tell you exactly how to speed things up in your case. Let me know if that helps.

Related Problems and Solutions