When loading a WebView in an Android App, it shows only a link to the page instead of the website contents next to the android logo on black background. After restarting the app, the content is loaded.
Java Code:
public class MainActivity extends Activity {
private WebView webView;
private String baseUrl = "http://www.example.com";
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.web_view);
webView.setId(10001);
webView.setBackgroundColor(0xFF000000);
// webView.setScrollBarStyle(SCROLLBARS_OUTSIDE_OVERLAY);
final Activity activity = this;
final ProgressDialog progressDialog = new ProgressDialog(activity);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setCancelable(false);
progressDialog.getWindow().setGravity(Gravity.BOTTOM);
hideProgressbarText(progressDialog);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
progressDialog.show();
progressDialog.setProgress(0);
activity.setProgress(progress * 1000);
progressDialog.incrementProgressBy(progress);
if (progress == 100 && progressDialog.isShowing())
progressDialog.dismiss();
}
});
webView.setWebViewClient(new WebViewClient());
// final ProgressBar progressBar= (ProgressBar)
// findViewById(R.id.progressbar);
WebSettings settings = webView.getSettings();
if (settings != null) {
settings.setJavaScriptEnabled(true);
settings.setDomStorageEnabled(true);
settings.setDefaultZoom(WebSettings.ZoomDensity.MEDIUM);
}
if (savedInstanceState == null) {
webView.loadUrl(baseUrl);
}
}
public void onBackPressed() {
if (webView.isFocused() && webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
finish();
}
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void hideProgressbarText(ProgressDialog progressDialog) {
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.HONEYCOMB) {
progressDialog.setProgressNumberFormat(null);
progressDialog.setProgressPercentFormat(null);
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
webView.saveState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
webView.restoreState(savedInstanceState);
}
}
Screenshot: