Skip to content Skip to sidebar Skip to footer

How To Set Progress Bar For Loading Activity With Data In Android

This is my first class doing Url loading . public class HelloWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, Str

Solution 1:

add this code to your code

ProgressDialog pd = new ProgressDialog(detailedview .this);
                                       ^^^^^^^^^^^^
        pd.setMessage("Loading...");
        pd.show(); <-----

mWebView.setWebViewClient(new WebViewClient() {
^^^^^^^^    

@Override 
public void onPageFinished(WebView view, String url) { <-----
     super.onPageFinished(view, url);
     pd.dismiss(); <-----
   }
});
-------------------------------------------------------------------------------


public class detailedview extends Activity
{
    WebView mWebView;
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.detailedview);
        ProgressDialog pd = new ProgressDialog(detailedview .this);

        pd.setMessage("Loading...");
        pd.show(); 

       mWebView.setWebViewClient(new WebViewClient() {


@Override 
public void onPageFinished(WebView view, String url) { 
     super.onPageFinished(view, url);
     pd.dismiss(); 
   }
});
        GetSet gs = new GetSet();

        String title = gs.getTitle();
        String desc = gs.getDesc();

        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setBuiltInZoomControls(true);
        mWebView.getSettings().setSupportZoom(true);
        mWebView.getSettings().setTextSize(TextSize.SMALLER);
        mWebView.loadDataWithBaseURL("", "<p  align=\"justify\"><b> " + title+"</p></b><p align=\"justify\"><br>"+ desc + "</p></br>", "text/html", "utf-8", "");
      //  mWebView.setWebViewClient(new HelloWebViewClient());
    }
}

Solution 2:

You can use below code

public class Grab extends Activity {

WebView view;
private ProgressDialog progressDialog = null;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.grab);

    view = (WebView) findViewById(R.id.WebView01);
    view.setBackgroundColor(0);  // set the background transparent 

    progressDialog = ProgressDialog.show(view.getContext(), "Loading...", "Loading...");
    progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER    );

    view.setWebChromeClient(new MyWebChromeClient());
    view.getSettings().setJavaScriptEnabled(true);
    view.setWebViewClient(new MyWebViewClient());
    view.loadUrl("http://www.wepware.com/web/apps/jots.do");
}


private class MyWebViewClient extends WebViewClient {
    @Override
     public boolean shouldOverrideUrlLoading(WebView view, String url)      //override WebViewClient method to open url in activity
    {
        view.loadUrl(url);
        return true;
    }
}

private class MyWebChromeClient extends WebChromeClient {
    @Override
    public void onProgressChanged(WebView view, int newProgress) {
        if(progressDialog!=null){
            if ( newProgress >=80 ) {
                progressDialog.dismiss();
            } else {
                progressDialog.setMessage(newProgress + " % loaded");
            }
        }
        super.onProgressChanged(view, newProgress);
    }
}
}

Solution 3:

first make one class say it DownloadWebPageTask

put this in ur oncreate method

GetSet gs = new GetSet();

    String title = gs.getTitle();
    String desc = gs.getDesc();

    mWebView = (WebView) findViewById(R.id.webview);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setBuiltInZoomControls(true);
    mWebView.getSettings().setSupportZoom(true);
    mWebView.getSettings().setTextSize(TextSize.SMALLER);

now make an object of that class like this

DownloadWebPageTask task = new DownloadWebPageTask(); task.execute(); Note:**task.execute(); will call ur class "DownloadWebPageTask" u can start this from any line of code write now i have put this in onCreate Method

and here is ur class

private class DownloadWebPageTask extends AsyncTask<String, Void, String> {

     private final ProgressDialog dialog = new ProgressDialog(Home.this);

      // can use UI thread here
      protected void onPreExecute() {
         this.dialog.setMessage("Loading...");
         this.dialog.show();
      }
    @Override
    protected String doInBackground(String... urls) {
        String response = "";

      mWebView.loadDataWithBaseURL("", "<p  align=\"justify\"><b> " + title+"</p></b><p align=\"justify\"><br>"+ desc + "</p></br>", "text/html", "utf-8", "");

        return response;
    }

    @Override
    protected void onPostExecute(String result) {
         if (this.dialog.isShowing()) {
                this.dialog.dismiss();
           }

             mWebView.setWebViewClient(new HelloWebViewClient());
    }


}   

Note:**Whenever u use any progress bar than in on post excute method all background process are done till than ur progress bar will be displyed and as soon as data downloading is completed than onPostExcute method will be call.and data downloaded from external server will be stored in webview or u can do any thing with that. If u still find any query than tell me Best Of luck Aamirkhan I.


Post a Comment for "How To Set Progress Bar For Loading Activity With Data In Android"