David_D
David_D

Reputation: 1402

Toast in webview not disappear onProgresseChanged

in my webview i want create a toast when the page is loading and when the progress is 100% i want that the toast disappear.. Actually doesn't work. This is the code

webview.setWebChromeClient(new WebChromeClient(){

                public void onProgressChanged(WebView view, int progress) {
                    activity.setTitle("Loading...");
                    activity.setProgress(progress * 100);
                    Toast toast = Toast.makeText(MainActivity.this, "Loading...",Toast.LENGTH_LONG);
                    toast.show();
                    if(progress == 100)
                    setProgressBarIndeterminateVisibility(false);
                    activity.setTitle("My WebView");
                    toast.cancel();
                }
            });

In this way the toast doesn't appear web i load a page... If i remove toast.cancel(); the toast appears but never disappears.. How can i solve?

Upvotes: 0

Views: 638

Answers (1)

Amit Gupta
Amit Gupta

Reputation: 8939

Try Like this

public void onProgressChanged(WebView view, int progress) {
                        activity.setTitle("Loading...");
                        activity.setProgress(progress * 100);

                        if(progress == 100)
                           {
                        Toast toast = Toast.makeText(MainActivity.this, "Finish...",Toast.LENGTH_LONG);
                        toast.show();
                           }
                        setProgressBarIndeterminateVisibility(false);
                        activity.setTitle("My WebView");
                    }
                });

Or else Try like this,

this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
            setContentView(R.layout.info);

            mProgressDialog = new ProgressDialog(this);
            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            mProgressDialog.setMessage("Please wait...");
            mProgressDialog.setCancelable(false);
            mProgressDialog.setMax(100);
            mProgressDialog.show();

            WebView webView = (WebView) findViewById(R.id.webview);
            webView.setVisibility(View.VISIBLE);
            webView.getSettings().setJavaScriptEnabled(true);

            webView.setWebChromeClient(new WebChromeClient() {
                public void onProgressChanged(WebView view, final int progress)
                {
                    activity.setTitle("Loading...");
                    activity.setProgress(progress * 100);
                    mProgressDialog.setProgress(progress );




                    if(progress == 100)
                    {
                        activity.setTitle(R.string.app_name);
                        mProgressDialog.dismiss();
                    Toast toast = Toast.makeText(MainActivity.this, "Finish...",Toast.LENGTH_LONG);
                        toast.show();
                    }

                }
            });

            webView.setWebViewClient(new WebViewClient() {
                @Override
                public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
                {
                }

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

Upvotes: 1

Related Questions