Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show ProgressBar for a certain time in Android

I have to wait some seconds in my Android App and I want to show a progress bar during this time, how can I do this?

I tried for example this code:

    public boolean WaitTask() {

        pDialog = ProgressDialog.show(context,null, "Lädt..",true);
        new Thread() {
        public void run() {
            try{
                // just doing some long operation
                sleep(2000);
             } catch (Exception e) {  }
             pDialog.dismiss();
             }
         }.start();
        return true;
   }

But the progressbar closes immediately without waiting the two seconds. Where is my problem?

The progressbar should look like the activity circle showing in this site from Android Developers.

UPDATE The AsyncTask

private class WaitTime extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mDialog.show();
    }
    protected void onPostExecute() {
        mDialog.dismiss();
    }

@Override
protected void onCancelled() {
    mDialog.dismiss();
    super.onCancelled();
}

    @Override
    protected Void doInBackground(Void... params) {
        long delayInMillis = 2000;
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                mDialog.dismiss();
            }
        }, delayInMillis);
        return null;
    }
}

I call it like this:

        mDialog = new ProgressDialog(CreateProject.this);
        mDialog = ProgressDialog.show(context,null, "Lädt..",true);

        WaitTime wait = new WaitTime();
        wait.execute();
like image 209
Mokkapps Avatar asked Oct 02 '12 13:10

Mokkapps


People also ask

How do I show percentage progress on android?

setMax(int max_value)– It sets the maximum value of the progress in the progress bar. setProgress(int prog_val)– It updates the progress to the progress value that is passed in it. show(Context context, CharSequence title, CharSequence msg)– It displays the progress bar.

Can you use to display a progress bar in an android application?

Progress bars are used to show progress of a task. For example, when you are uploading or downloading something from the internet, it is better to show the progress of download/upload to the user. In android there is a class called ProgressDialog that allows you to create progress bar.

What is a difference between SeekBar and ProgressBar in android?

A SeekBar is an extension of ProgressBar that adds a draggable thumb. The user can touch the thumb and drag left or right to set the current progress level or use the arrow keys. Placing focusable widgets to the left or right of a SeekBar is discouraged.


1 Answers

I reccomend you to use AsyncTask, then you can do something like this:

    AsyncTask<Void, Void, Void> updateTask = new AsyncTask<Void, Void, Void>(){
        ProgressDialog dialog = new ProgressDialog(MyActivity.this);
        @Override
        protected void onPreExecute() {
            // what to do before background task
            dialog.setTitle("Loading...");
            dialog.setMessage("Please wait.");
            dialog.setIndeterminate(true);
            dialog.setCancelable(false);
            dialog.show();
        }

        @Override
        protected Void doInBackground(Void... params) {
            // do your background operation here
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            // what to do when background task is completed
            dialog.dismiss();
        };

        @Override
        protected void onCancelled() {
            dialog.dismiss();
            super.onCancelled();
        }
    };
    updateTask.execute((Void[])null);

and if you want to wait for some specific time, maybe you would like to use Timer:

    final ProgressDialog dialog = new ProgressDialog(MyActivity.this);
    dialog.setTitle("Loading...");
    dialog.setMessage("Please wait.");
    dialog.setIndeterminate(true);
    dialog.setCancelable(false);
    dialog.show();

    long delayInMillis = 5000;
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            dialog.dismiss();
        }
    }, delayInMillis);
like image 189
Berťák Avatar answered Oct 11 '22 15:10

Berťák