1

In my app i want to have a delay of 5 seconds and in this five seconds user should see progress dialog i tried this

    progressdialog.show();
  try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    progressdialog.dismiss();

but while Thread is sleeping the progessdialog also wont show .

Manohar
  • 22,116
  • 9
  • 108
  • 144
  • Whats the problem with writing code in onFinish? – user6547359 Jul 12 '16 at 11:20
  • use thread instead of current thread ,currently you are sleeping the current ui thread. – mcd Jul 12 '16 at 11:21
  • i used delay about 15-20 times in code so there will be lots of changes i have to make . so i am searching for alternate method – Manohar Jul 12 '16 at 11:26
  • Well as mcd said, you have to use anotehr thread, I'd use a Handler to postDelayed, put the code into a method and call the method in the runnable run method. – user6547359 Jul 12 '16 at 11:27

2 Answers2

3
 new CountDownTimer(6000, 1000) {
            public void onFinish() {
               mProgressDialog.dismiss();
              // my whole code
            }

            public void onTick(long millisUntilFinished) {
                mProgressDialog.show();
            }
        }.start();

This works fine

Manohar
  • 22,116
  • 9
  • 108
  • 144
1
        progressDialog.show();

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                progressDialog.dismiss();
            }
        },3000);
Latief Anwar
  • 1,833
  • 17
  • 27
  • 2
    Please don't post only code as an answer, but include an explanation what the code does and how it solves the problem of the question. Answers with an explanation are generally of higher quality, and more likely to attract upvotes. – Mark Rotteveel Oct 23 '19 at 06:36