Add progressbar to my webview

Asked

Viewed 1,640 times

1

Hello, I want to add a progress bar to my webview.

I saw a tutorial and it didn’t work properly, because the progress bar keeps loading eternally.

I will post the code of how it was, I wanted to appear the progress bar only when loading the page, code I have:

public class MainActivity extends Activity {

    private ProgressBar pb;
    private WebView wv;
    private String URL = "http://www.google.com";

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ActionBar actionBar = getActionBar();
        actionBar.hide();
        wv = (WebView) findViewById(R.id.webView1);
        pb = (ProgressBar)findViewById(R.id.progressBar1);

        carregarSite();

    }


    public void carregarSite() {

        try {
            // Verifica conexão
            if (verificaConexao()) {
                // Ajusta algumas configurações
                WebSettings ws = wv.getSettings();
                ws.setJavaScriptEnabled(true);
                ws.setBuiltInZoomControls(true);
                wv.setWebViewClient(new WebViewClient());
                wv.setWebChromeClient(new WebChromeClient());
                wv.loadUrl(URL);
            }

            else
                msgConexao();
        } catch (Exception e) {
            Toast.makeText(MainActivity.this, "Erro ao carregar o site", 1000)
                    .show();
        }
    }

    @Override
    public void onBackPressed() {

        msgExit();

    }

    private class MyWebViewClient extends WebViewClient {   
         @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

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

         @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {

            super.onPageStarted(view, url, favicon);
        }

    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if((keyCode == KeyEvent.KEYCODE_BACK) && wv.canGoBack())
        {
            wv.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}

Layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"

    tools:ignore="MergeRootFrame" >

    <WebView
        android:id="@+id/webView1"
        android:layout_width="match_parent"
        android:layout_height="357dp"
        android:layout_weight="1.78" />


    <ProgressBar
        android:id="@+id/progressBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         android:layout_gravity="center"
         />

</LinearLayout>

2 answers

1

Built-in style

You can add a progress bar to WebView by defining the following code in the method onCreate():

// solicitar a barra de progresso para a activity
getWindow().requestFeature(Window.FEATURE_PROGRESS);

// definir um WebChromeClient para acompanhar o progresso    
wv.setWebChromeClient(new WebChromeClient()
{
 public void onProgressChanged(WebView view, int progress)
 {
   // atualizar a barra de progresso
   MainActivity.this.setProgress(progress * 100);
 }
});

This lets you view the progress bar at the top of the screen with the built-in Android style.

Personalized

For a custom progress bar, the process is the same, you need to get a handler:

ProgressBar pb = (ProgressBar)findViewById(R.id.progressBar1);

And then on the job onProgressChanged() should update the progress:

pb.setProgress(progress * 100);

Withdrawal solution of this answer SOEN user-placed @Csmith.

  • I put the code you spoke before loading methodSite(); but androidRuntimeException error

0

I wore it like this, I don’t know if it’ll work!

    final ProgressBar Pbar;
    Pbar = (ProgressBar) findViewById(R.id.progressBar);


    myWebView.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress)
        {
            if(progress < 100 && Pbar.getVisibility() == ProgressBar.GONE){
                Pbar.setVisibility(ProgressBar.VISIBLE);
            }
            Pbar.setProgress(progress);
            if(progress == 100) {
                Pbar.setVisibility(ProgressBar.GONE);
            }
        }
    });

Browser other questions tagged

You are not signed in. Login or sign up in order to post.