tags:

views:

106

answers:

1

Hello In my android application,i would like to place a progress bar such that it shows the user that the data is getting downloaded and gets dismissed once the data is loaded.

Is there any way that i can achieve this.

Thanks in advance:)

A: 

You can achieve it by AsyncTask class.

In that three steps you have to follow,

  1. you have to start the ProgreesDialog in onPreExecute().
  2. doInBackground() takes control of the Downloading Progress.
  3. onPostExcecute() runs after the second step. on that you can dismiss your progressdialog, start the New Activity and finish your splashscreen.

More Info, check the Documentation. It has a explanation with example code.

CODE:

  private class Task extends AsyncTask<Void, Void, Void> {
    private final ProgressDialog dialog = new ProgressDialog(
            your_class.this);

    // can use UI thread here
    protected void onPreExecute() {
        this.dialog.setMessage("Loading...");
        this.dialog.setCancelable(false);
        this.dialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        try {
            // do downloading images code here
        } catch (Exception e) {

        }
        return null;

    }

    protected void onPostExecute(Void result) {
           //start the another activity and then close your current activity here.
        if (this.dialog.isShowing()) {
            this.dialog.dismiss();
        }
    }
}
Praveen Chandrasekaran
Thanks Praveen.Could you please provide me with any sample code if u have?
Remmyabhavan
@Reshmi: Please check the documentation link. I mentioned it already in my answer.
Praveen Chandrasekaran
Thanks Praveen.The sample code is to download file from url but what require is download file from local project.Is it possible with the same.
Remmyabhavan
Sorry if the my doubt is very silly but as i am newbie couldnot make it
Remmyabhavan
@Reshmi: Hey i added some code snippet. Hope it helps you. and one more thing, when you comment to anybody. comment like this format `@username: your comment`. It wake up the notification for them. it helps to get the quick response. ;)
Praveen Chandrasekaran
@Praveen.Thanks Praveen.It works.Thanks for detail info.
Remmyabhavan