Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android webview loading dialog

I'm trying to implement a loading dialog in my webview application but whatever I do it just won't work.

What I want is the following, everytime someone clicks a link in the webview the loading dialog has to popup like this:

loading dialog
(source: wordpress.com)

And when the loading is done it has to disappear. Can anyone show me how to do it in my own source? I've tried a lot of things but there's always an issue so I really don't know how to.

This is my WebviewActivity:

import android.content.res.Configuration;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class WebviewActivity extends MainActivity {

    private WebView myWebView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);

        myWebView = (WebView)findViewById(R.id.webview);        
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);        
        myWebView.setWebViewClient(new WebViewClient());

        myWebView.requestFocus(View.FOCUS_DOWN);
        myWebView.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                    case MotionEvent.ACTION_UP:
                        if (!v.hasFocus()) {
                            v.requestFocus();
                        }
                        break;
                }
                return false;
            }
        });
    }   

    @Override
    public void onResume() {
        super.onResume();
        if ( isOnline() == true )
            myWebView.loadUrl(webLink);
        else if ( isOnline() == false )
            showNoConnectionDialog();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig){        
        super.onConfigurationChanged(newConfig);
    }
}

Thank you very much for your help!!

like image 644
MartijnG Avatar asked Jul 03 '26 05:07

MartijnG


1 Answers

You have to override the WebViewClient like this,

      webview.setWebViewClient(new WebViewClient(){

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

                progressDialog.cancel();


            }
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {

                progressDialog.show();
            }
      });

webview.loadUrl(TwitterUrl);
like image 117
Andro Selva Avatar answered Jul 04 '26 20:07

Andro Selva



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!