2

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?

for example :

@Override
protected void onCreate(Bundle savedInstanceState) {

    timeForShow(5 //second);

}
.
.
.

private void timeForShow(long mytime){

myprogress.setVisibility(View.VISIBLE);   
Waiting for mytime...
myprogress.setVisibility(View.GONE); 

}

this is my code but it does not work:

Long timerforprogressbar ;

@Override
protected void onCreate(Bundle savedInstanceState) {

    timerforprogressbar = (long) 5000 ;
    new MyProgressBar().execute((Void)null);

}
.

.

.


class MyProgressBar extends AsyncTask<Void, Void, Void>{

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub
        progressbar.setVisibility(View.VISIBLE);
        try {
            Thread.sleep(timerforprogressbar);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        progressbar.setVisibility(View.GONE);
    }

}

my progress bar :

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"
        android:indeterminateDrawable="@drawable/progress" >
    </ProgressBar>

progressbar is my progress bar,plz help me,tnx.

user3103823
  • 135
  • 1
  • 3
  • 13
  • I tried to give an answer but I would like some more detail of your problem. You say it does not work.. What is it doing wrong? Is the progressbar not showing, or how does it not behave as you expect? – cYrixmorten Jan 30 '14 at 21:21
  • I have a asynctask : http://paste.ubuntu.com/6846131/ . it is a parsing xml. now, I created a function : http://paste.ubuntu.com/6846146/ . I wanted to show my progress for second time (custom time). But my progress can not be seen ! – user3103823 Jan 30 '14 at 21:41
  • First of all, doInBackground is running in a background thread, no need to have executeThread. Second, as I mention in my answer, try and create the progressdialog programatically instead, ill make an edit to my answer demonstrating it in a few moments. – cYrixmorten Jan 30 '14 at 22:01
  • Alternatively, if you do not want a dialog, I suspect setting visibility to invisble instead of gone might help. – cYrixmorten Jan 30 '14 at 22:08

6 Answers6

5

For your specific use case it would be simpler to use a Handler:

new Handler().postDelayed(new Runnable() {

    @Override
    public void run() {
       progressbar.setVisibility(View.GONE);
    }

}, timerforprogressbar);

You did not mention how your progressbar is constructed but if you simply want an indeterminate progress to show, then constructing it programatically would be the most simple thing to do I think.

private ProgressDialog working_dialog;

private void showWorkingDialog() {
    working_dialog = ProgressDialog.show(context, "","Working please wait...", true);
}

private void removeWorkingDialog() {
    if (working_dialog != null) {
        working_dialog.dismiss();
        working_dialog = null;
    }
}

That would make the code look like:

showWorkingDialog();

new Handler().postDelayed(new Runnable() {

    @Override
    public void run() {
        removeWorkingDialog();
    }

}, timerforprogressbar);
cYrixmorten
  • 7,110
  • 3
  • 25
  • 33
1

You can always use the CountDownTimer:

public void timer() {

    countDownTimer=new CountDownTimer(20000, 1000) { //display a delay of 20 seconds

        public void onTick(long millisUntilFinished) {


            if (somethingToBreakTheCount)
                countDownTimer.onFinish();

            txtDebug.setText("seconds remaining: " + millisUntilFinished / 1000);
        }

        public void onFinish() {
            txtDebug.setText("Done!");
            finish();
        }
    }.start();
}
1

// We can achieve this by using Coroutines.
// Simply start a coroutine and change the progress of progressbar by changing the dispatcher context after few milliseconds.
// Here I am showing the progress for 1 second before moving to next fragment.

CoroutineScope(Dispatchers.IO).launch {
                for (i in 0..100) {
                    delay(10)
                    withContext(Dispatchers.Main) {
                        binding.progressBar.progress = i
                    }
                }
        //Navigate to next fragment using nav contorller or with Intents for next Activity
        navController.navigate(R.id.action_startFragment_to_listFragment)

}
Chinmay
  • 354
  • 4
  • 10
0

Updating progress dialog in Activity from AsyncTask

There re is a method for async tasks for progress updates.

Here is a link to a better post on it

Community
  • 1
  • 1
AdamM
  • 162
  • 1
  • 8
0

As a simpler solution than an AsyncTask, you can always look into using a CountDownTimer for this action.

GrouchyPanda
  • 1,004
  • 8
  • 20
0

If you are performing a task in AsyncTask doInBackground(), then you can initiate your progressBar in onPreExecute() and disable it in onPostExecute().

android_dev_
  • 412
  • 2
  • 6