Reputation: 180
I am working on an application where i am loading a webpage from a an external url in a webview. Loading the page take so much time to be loaded it take between 30 Seconds and 1 Minute so please take look here on my code
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 idea to improve the performance ?
Upvotes: 5
Views: 14877
Reputation: 1272
While loading URLs in your webview if you put all your static resources like CSS, JS fonts etc in your android app's local assets folter and give relative URL to your resource in page then it will load much faster.
This is because right now your webview is loading all the resource from your or third party server wherever they exists but if they are in assets folder then you can load them locally.
So over all your data and HTML will come from your server and static content will be stored and loaded locally.
You have use loadDataWithBaseURL method of webview insteed of loadUrl
Here is a detailed article which can be helpful http://www.codewithasp.net/2016/06/speedup-android-webview-app-localise-static-resources.html
Upvotes: 1
Reputation: 12328
Unofortunately, there won't be much you can do to fix this. However:
There's a couple of things you can try though, and a few things to check. Specifically:
You're setting the visibility to View.GONE
(making your webview invisible) while the page is loading, and then making it visible again when the page has loaded. This is probably the problem.
Try without this, and you will probably find that it will be quicker. In my experience, onPageFinished(..)
only fires some time after the page is loaded.
Does the page really require JavaScript ? If not, don't enable it.
If it's feaseable in your case, you can use a HTML parser like Jsoup to extract only the desired data from the page, and show that to the user. This will be a lot faster.
If the page uses Ajax to load data dynamically, you can also load the data directly from the endpoints it uses. Open the page in a desktop browser, and open the network tab of developer tools to find out how the page works and loads data.
You can block requests from the WebView with shouldInterceptRequest(..)
. This may help if the page has things like eg. Facebook share buttons or extra images which you don't need. Blocking these will speed up load times.
If you show us the URL you're using, maybe I can investigate more and tell you axactly how you could speed it up in your case. Let me know if it helps.
Upvotes: 7
Reputation: 445
I think depends on the amount of data that need to download . Also keep in mind that while you are in debug mode the application is much slower as it has to to trace all information.
Maybe this links can help you to improve performance:
Upvotes: 2