Adding a Progress Dialog in a webview

24,655

Solution 1

Check this code,

    final ProgressDialog pd = ProgressDialog.show(OnlinePaymentActivity.this, "", "Please wait, your transaction is being processed...", true);
            
    WebView mWebview  = (WebView)findViewById(R.id.webView1);
    mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
    mWebview.getSettings().setLoadWithOverviewMode(true);
    mWebview.getSettings().setUseWideViewPort(true);
    mWebview.getSettings().setBuiltInZoomControls(true);

    mWebview.setWebViewClient(new WebViewClient() {

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            pd.show();
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            pd.dismiss();
            String webUrl = mWebview.getUrl();
        }

    });

    mWebview .loadUrl("www.google.com");

Solution 2

Webview with progress dialog working for me. full working code

import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.yubrajpoudel.R;

/**
 * Created by yubraj on 12/25/15.
 */
public class NewsActivity extends AppCompatActivity {
    private WebView webView;
    ProgressDialog prDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page_news);
        setSupportActionBar((Toolbar) findViewById(R.id.toolbar));

        webView = (WebView) findViewById(R.id.wv_news);
        webView.setWebViewClient(new MyWebViewClient());

        String url = "http://google.com/";
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        webView.loadUrl(url);


    }

    private class MyWebViewClient extends WebViewClient {

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

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            prDialog = new ProgressDialog(NewsActivity.this);
            prDialog.setMessage("Please wait ...");
            prDialog.show();
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            if(prDialog!=null){
                prDialog.dismiss();
            }
        }
    }
}
Share:
24,655
bigC5012
Author by

bigC5012

Enthusiast.

Updated on June 05, 2022

Comments

  • bigC5012
    bigC5012 almost 2 years

    I have been trying to incorporate a progress dialog into my app when pages are loading in the webview so there isn't just a blank white screen. I know this question is posted everywhere but I can't seem to figure out anything that works for me. I am new to programming and working with android so any information would be helpful. Below is the code that I currently have now. With the onPageStarted I am getting a compile error for Bitmap and I'm not sure what the problem is. Thanks.

    public class Grades extends Activity{
    
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);  
    
    
        //requesting system settings
    
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().requestFeature( Window.FEATURE_PROGRESS);
         WebView webview = new WebView(this);
    
         //web settings
    
        WebSettings webSettings = webview.getSettings();
            webSettings.setJavaScriptEnabled(true); 
            webSettings.setDisplayZoomControls(true);
            webSettings.setBuiltInZoomControls(true);
    
    
         setContentView(webview);
    
        //loads webpages in webview instead of launching browser
    
         webview.setWebViewClient(new WebViewClient() {
                ProgressDialog prDialog;
                @Override
                public void onPageStarted(WebView view, String url, Bitmap favicon) {
                    prDialog = ProgressDialog.show(Grades.this, null, "loading, please wait...");
                    super.onPageStarted(view, url, favicon);
                }
    
                @Override
                public void onPageFinished(WebView view, String url) {
                    prDialog.dismiss();
                    super.onPageFinished(view, url);
                }
            });
    
       //loading webpage
         webview.loadUrl("page__link");
    }