Thushara prasad
Thushara prasad

Reputation: 165

adding custom progress bar in android webview

I need a progress bar at the centre of my android webview it must be shown before the page loads and hides when the page is loaded. How can it be done? Can anyone help me?

This is my code for webview:

 Bundle extras = getIntent().getExtras();
String title;
final String url;

if (!Datacon.checkInternetConnection(this)) {
    Toast.makeText(getApplicationContext(), "Check your Internet Connection!", Toast.LENGTH_LONG).show();
} else {

if (extras != null) {
    title = extras.getString("title");
    url = extras.getString("url");
    TextView text=(TextView) findViewById(R.id.textView1);
    text.setText(title);
    final WebView myWebView =(WebView)findViewById(R.id.WebView);
    myWebView.loadUrl(url);
    myWebView.getSettings().setLoadWithOverviewMode(true);
    myWebView.getSettings().setUseWideViewPort(true);
    myWebView.getSettings().setBuiltInZoomControls(true);
    myWebView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN); 
    loadingProgressBar=(ProgressBar)findViewById(R.id.progressbar_Horizontal); 

    myWebView.setWebViewClient(new WebViewClient(){


        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return false;
        }
    });

Button refresh = (Button) actionBar.getCustomView().findViewById(R.id.but2);
refresh.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        myWebView.loadUrl(url);

        }
    });

} 
}}

}

Upvotes: 1

Views: 573

Answers (2)

Dhruba Bose
Dhruba Bose

Reputation: 446

private void initializeProgressBar() {
if (progressBar == null){
progressBar = new ProgressDialog(this);
progressBar.setMessage(Constants.LOADING_MESSAGE);
progressBar.setCancelable(false);
}
}

Handler peogressBar = new Handler(){
public void handleMessage(android.os.Message msg) {
try{
switch (msg.what) {
case 1:
initializeProgressBar();
if(!progressBar.isShowing())
progressBar.show();
break;
case 2:
if (progressBar != null && progressBar.isShowing()) {
progressBar.dismiss();
progressBar = null;

}
break;
}
}catch(Exception e){
e.printStackTrace();
}
};
};



wv_graphLink = (WebView) findViewById(R.id.wv_graphLink);
wv_graphLink.getSettings().setJavaScriptEnabled(true);
wv_graphLink.getSettings().setBuiltInZoomControls(true);
wv_graphLink.getSettings().setSupportZoom(true);
wv_graphLink.setInitialScale(1);
wv_graphLink.getSettings().setLoadWithOverviewMode(true);
wv_graphLink.getSettings().setUseWideViewPort(true);
// wv_graphLink.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
// wv_graphLink.setScrollbarFadingEnabled(false);
peogressBar.sendEmptyMessage(1);
wv_graphLink.setWebChromeClient(new WebChromeClient(){

public void onProgressChanged(WebView view, int progress) {

if(progress == 100){
peogressBar.sendEmptyMessage(2);


}

}
});

wv_graphLink.loadUrl("Your url");

Upvotes: 0

Nilzor
Nilzor

Reputation: 18573

You need to override a couple of more methods in the WebViewClient :

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        loadingProgressBar.setVisibility(View.VISIBLE);
    }

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

I'm assuming loadingProgressBar is a class field.

PS: As a side note I believe you overriding shouldOverrideUrlLoading is unnecessary. The default behaviour is to load the url, as you instruct it to do.

Upvotes: 1

Related Questions