0

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:

enter image description here

Martin Schlagnitweit
  • 2,042
  • 6
  • 26
  • 42

2 Answers2

1

Seems like what you've got is the error page in case of e.g. page_not_found or DNS_failure. i.e. What you saw is the error messeage or the prompt text, which should be in BLACK color. However you just set your background color as 0xFF000000 (as same the text color) which will hide the prompt text.

Remove the black background and you may see the text. I suppose what's shown now is not your page content but some error prompt from webkit engine.

Wei WANG
  • 1,748
  • 19
  • 23
  • You are right. the problem occured if no internet connection was available. I solved it using this question: http://stackoverflow.com/questions/3767591/check-intent-internet-connection – Martin Schlagnitweit Jan 29 '14 at 18:17
0

Use this Website for webview reference they explained good manner

Make sure below

<uses-permission android:name="android.permission.INTERNET" />
learner
  • 3,092
  • 2
  • 21
  • 33
  • use two activity ,create button in 1st activity if you click the button it will redirect to second activity,maintain 2nd activity is webview activity,it will work – learner Jan 28 '14 at 18:00