Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a Progress Dialog in a webview

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");
}
like image 868
bigC5012 Avatar asked May 31 '13 03:05

bigC5012


People also ask

How do I display progress dialog?

ProgressDialog is a modal dialog, which prevents the user from interacting with the app. Instead of using this class, you should use a progress indicator like ProgressBar , which can be embedded in your app's UI. Alternatively, you can use a notification to inform the user of the task's progress.

Why is progress dialog deprecated?

A ProgressDialog would hold a ProgressBar inside an Alert Dialog. ProgressDialog is now deprecated since it isn't a good idea to show long progress in a dialog while blocking the screen.

How do I send a post request in WebView?

String url = "http://example.com/php.php"; String postData = null; postData = "param1=" + URLEncoder. encode("1234567890", "UTF-8"); webcontent. postUrl(url,postData. getBytes()); //or webcontent.


1 Answers

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");
like image 133
Basim Sherif Avatar answered Oct 06 '22 00:10

Basim Sherif