2

Simply put how can I make a progress dialog show up every time a new link is clicked in my webview. I have tried many tutorials and methods but every one of them only shows the dialog once, when the app is initially loaded.

Here is my code

 mWebView.setWebViewClient(new WebViewClient() {

             @Override
             public boolean shouldOverrideUrlLoading(WebView view, String url)
             {
                 view.loadUrl(url);
                 return true;
             }
            ProgressDialog dialog = ProgressDialog.show(myActivity.this, "", 
                            "Loading. Please wait...", true);
            @Override
            public void onPageFinished(WebView view, String url) {
             dialog.dismiss();
           }
Zach
  • 1,964
  • 2
  • 17
  • 28
  • It would be helpful if you could specify what you tried already. – Sasha Goldshtein Jan 25 '12 at 11:29
  • Quite a lot of talk on StackOverflow about this: Here http://stackoverflow.com/questions/2537454/android-webview-progress-bar and here http://stackoverflow.com/questions/2496119/in-an-android-app-i-would-like-to-show-a-progress-bar-on-a-child-tab-until-the-w for example – nwaltham Jan 25 '12 at 11:31
  • I know, people are having issues with it everywhere. My problem is that I do not want a progressBar in the title. I can create a dialog easily if I attach it to an onClickListener to a button. I just have no idea how to get the app to realize when the url of the page is changing and display the dialog at that point – Zach Jan 25 '12 at 11:42
  • Seems to your problem, WebViewClient's shouldOverrideUrlLoading() method get triggered when any url is clicked on webpage, and when a webpage is finished for loading then onPageFinished() will triggered.. – user370305 Jan 25 '12 at 11:47

3 Answers3

4

check out this:

wvCouponsAndOffers.setWebViewClient(new WebViewClient() {
        ProgressDialog progressDialog = new ProgressDialog(CouponsWebViewUI.this);

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            progressDialog.setMessage(StringConstants.PROGRESS_DIALOG_MSG);
            progressDialog.setCancelable(false);
            progressDialog.setOnKeyListener(new OnKeyListener(){
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode,
                        KeyEvent event) {
                    if(keyCode == KeyEvent.KEYCODE_SEARCH) {
                        return true;
                    }
                    else
                        return false;
                }});
            progressDialog.show();
        }
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            progressDialog.setMessage(StringConstants.PROGRESS_DIALOG_MSG);
            progressDialog.setCancelable(false);
            progressDialog.setOnKeyListener(new OnKeyListener(){
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode,
                        KeyEvent event) {
                    if(keyCode == KeyEvent.KEYCODE_SEARCH) {
                        return true;
                    }
                    else
                        return false;
                }});
            progressDialog.show();
            view.loadUrl(url);
            return true;
        }

        public void onPageFinished(WebView view, String url) {
            if (progressDialog.isShowing()) {
                progressDialog.dismiss();
            }
        }

    });
Vicky Kapadia
  • 6,025
  • 2
  • 24
  • 30
3

Try this,

 // Let's display the progress in the activity title bar, like the
 // browser app does.
 getWindow().requestFeature(Window.FEATURE_PROGRESS);

 webview.getSettings().setJavaScriptEnabled(true);

 final Activity activity = this;
 webview.setWebChromeClient(new WebChromeClient() {
   public void onProgressChanged(WebView view, int progress) {
     // Activities and WebViews measure progress with different scales.
     // The progress meter will automatically disappear when we reach 100%
     activity.setProgress(progress * 1000);
   }
 });
 webview.setWebViewClient(new WebViewClient() {
   public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
     Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
   }
 });

 webview.loadUrl("http://slashdot.org/");

And if you want to make your own progressDialog then

 webView.setWebViewClient(new WebViewClient() {

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

       @Override
       public void onPageFinished(WebView view, String url) {

        //HIDE PROGRESS DIALOG LOADING IT HAS FINISHED
      }

    });

And let me know what happen..

user370305
  • 108,599
  • 23
  • 164
  • 151
  • I am having issues starting the progress dialog in that spot. I added the source code.. This is the only way I can get it to run but it only runs when the app is first loaded – Zach Jan 25 '12 at 12:23
  • @user1166556 - You are doing wrong make a ProgressDialog object in your activity's onCreate() and just display it in shouldOverrid... and dismiss on pageFinished() – user370305 Jan 25 '12 at 12:29
0

Try this easiest one,it work for me

public class HomeWebViewActivity extends AppCompatActivity{

    private String weburl;
    private WebView webview;
    private Toolbar toolbar;
    private ProgressDialog prDialog;



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_webview_activity);


         toolbar = (Toolbar) findViewById(R.id.toolbar_homewebview);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);


        weburl="ADD YOUR URL HERE";

        webview = (WebView) findViewById(R.id.webviewinfo);
        webview.setWebViewClient(new WebVwClientcls());


        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        webview.loadUrl(weburl);


    }

    private class WebVwClientcls 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(HomeWebViewActivity.this);
            prDialog.setMessage("Please wait ...");
            prDialog.show();
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            if(prDialog!=null){
                prDialog.dismiss();
            }
        }
    }
}
Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96