Progress bar or wait message, webview Android Studio

3

I have an activity with a webview where I show an external web page, this occasionally delays loading so I would like to put a progress bar or a message, this to avoid the user thinking that more than delay is that the application does not It's working.

This is how I implemented the WebView:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.portal_web);

    String url="http://www.uniagustiniana.edu.co/";
    WebView view=(WebView) this.findViewById(R.id.webView01);
    view.getSettings().setJavaScriptEnabled(true);
    view.loadUrl(url);
}
    
asked by Ivan Alfredo 12.05.2017 в 23:53
source

2 answers

3

You can do this by implementing a WebViewClient to your WebView and using the onPageFinished () determines that the page has been fully loaded, at which point you delete the dialog using the method dismiss() of ProgressBar .

    final ProgressDialog progressDialog = new ProgressDialog(this);
    progressDialog.setIcon(R.mipmap.ic_launcher);
    progressDialog.setMessage("Cargando...");
    progressDialog.show();

    //Obtiene referencia en Layout de WebView.
    webView = (WebView) findViewById(R.id.myWebView);
    //Carga página.
    webView.loadUrl("http://es.stackoverflow.com");
   //Define WebViewClient() para poder leer eventos que ocurren durante el cargado de contenido en el WebView.
    webView.setWebViewClient(new WebViewClient(){
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            //elimina ProgressBar.
            progressDialog.dismiss();
        }
    });

When the WebView is in the process of loading the content, the ProgressBar will be shown, the term disappears.

You can even change the style to look like a progress bar:

 progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

As another option, you can even implement a Asynctask to show the loading progress.

How to display a ProgressBar while getting a response from the server?

    
answered by 13.05.2017 / 01:50
source
3

What you are trying to do is not complicated, you just have to overwrite the OnPageFinished method

  private void showProgressBar(){

     mWebView = (WebView) findViewById(R.id.activity_main_webview);
    //ProgressDialog
    final ProgressDialog dialog=new ProgressDialog(MainActivity.this);
    dialog.setMessage("Espera...");
    dialog.show();

    mWebView.setWebViewClient(new WebViewClient(){
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            dialog.dismiss();
        }

    });
}
    
answered by 13.05.2017 в 00:09