Showing posts with label Progress Dialog in android. Show all posts
Showing posts with label Progress Dialog in android. Show all posts

Tuesday, July 22, 2014

How To Show Progress Dialog or Loader in WebView link inside a Fragment using Android

Progress Dialog or Loader in WebView link inside a Fragment


public class ActivityFragment extends Fragment {
    Context contxt;
    ProgressDialog progresDialog;
    WebView webView;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
       
        super.onActivityCreated(savedInstanceState);
        contxt=this.getActivity();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_activity, container,
                false);

         webView = (WebView) rootView.findViewById(R.id.webView1);

        String url ="http://base.com.bd";
        webView.setWebChromeClient(new WebChromeClient());
        webView.setWebViewClient(new MyWebViewClient(){

         
            @Override
            public void onPageFinished(WebView view, String url) {
                               
                try {
                    if(progresDialog.isShowing()){
                       
                        progresDialog.dismiss();
                        progresDialog=null;
                    }
                   
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
               
                if(progresDialog==null)
                {
                    progresDialog=new ProgressDialog(contxt,R.style.MyTheme);
                    progresDialog.setMessage("Loading... ");
                    progresDialog.show();
                   
                }
               
                webView.loadUrl(url);
                return true;
            }
           
           
        });

        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl(url);

        return rootView;

    }

    public class MyWebViewClient extends WebViewClient {

        public MyWebViewClient() {
            super();
                  }

         //On WebView Page Starting Loader

        public void onPageStarted(WebView webView, String url, Bitmap favicon) {

 if (progresDialog== null) {
            // in standard case YourActivity.this
            progresDialog= new ProgressDialog(contxt, R.style.MyTheme);

            progresDialog.setMessage("Loading...");
            // progressDialog.
            progresDialog.show();

        }

        }
    }
}