How to show full screen loading spinner for webview in android Fragment?

前端 未结 1 1260
野的像风
野的像风 2021-01-25 20:59

I am developing an app that has a fragment to show website on the webview. For slow connection it takes times to load the website and the app gets blank for few seconds so I wan

相关标签:
1条回答
  • 2021-01-25 21:41

    In your onCreateView() you need to show a ProgressBar, since your webView.loadUrl(url) takes time to load the internet content on your WebView. So you need to register for a call back when your WebView is done loading your content. You can listen like this :

    mWebView.setWebViewClient(new WebViewClient() {
    
       public void onPageFinished(WebView view, String url) {
            // Cancel your ProgressBar here
        }
    });
    

    Inside your onPageFinished() cancel your ProgressBar since your WebView has completed loading the content.

    WORKING CODE

    public class MainActivity extends Activity {
    
        private String url = "https://www.google.co.in/";
        ProgressDialog pb;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            pb = new ProgressDialog(this);
            pb.setTitle("Connecting Station");
            pb.setMessage("Please Wait....");
            pb.setCancelable(false);
            pb.show();
            WebView webView = (WebView) findViewById(R.id.webView);
            WebSettings webViewSettings = webView.getSettings();
            webViewSettings.setJavaScriptCanOpenWindowsAutomatically(true);
            webViewSettings.setJavaScriptEnabled(true);
            webViewSettings.setPluginState(PluginState.ON);
            webView.loadUrl(url);
            webView.setWebViewClient(new WebViewClient() {
    
                public void onPageFinished(WebView view, String url) {
                    pb.dismiss();
                }
            });
        }
    }
    

    enter image description here

    0 讨论(0)
提交回复
热议问题