1

I have to display different messages in progress dialog, when running in async task.

First I need to the display the message "Please wait", then "Downloading from server", then "Please wait for sometime".

I have tried with publishProgress but when I run the application, on my ProgressDialog, only the last message "Please wait for sometime" is displayed. How can I display the three messages?

private class Sample extends AsyncTask<String, String, String> {
    ProgressDialog testdialog;

    @Override
    protected void onPreExecute() {
        testdialog = new ProgressDialog(test.this);
        testdialog.setTitle("Title");
        testdialog.setMessage("Please wait ");
        testdialog.setIndeterminate(false);
        testdialog.setCancelable(false);
        testdialog.setCanceledOnTouchOutside(false);
        testdialog.show();
    }

    @Override
    protected String doInBackground(String... urls) {
        publishProgress("Downloading from server");
        publishProgress("Please wait for sometime");
        /* here I code the background downloading process*/
    }

    @Override
    protected void onProgressUpdate(String... pro) {
        testdialog.setMessage(pro[0]);
        testdialog.setMessage(pro[1]);
    }

    @Override
    protected void onPostExecute(String result) {
        testdialog.dismiss();
    }
}
Gokul Rajkumar
  • 115
  • 2
  • 13
  • it's happening very fast, you don't notice, so last message only appears, tyr to add some delay (sleep) between the 2 calls of `publishProgress()` like 2 seconds, you should notice 2 messages on the progressDialog – Yazan Mar 09 '16 at 09:31
  • @Gokul Rajkumar **try these link all are on same thread** http://stackoverflow.com/questions/7970113/change-a-dialogs-message-while-using-asynctask **also** http://stackoverflow.com/questions/23961605/android-progress-dialog-publishprogress-method – Sandeep Devhare Mar 09 '16 at 10:03
  • @Yazan Thanks for ur responce, when i tried with adding delay for each of the publishProgress the screen goes blank, can u give some better answer or any sample to show the different messages in progressdialogues. – Gokul Rajkumar Mar 09 '16 at 10:57

1 Answers1

2

try this code in doInBackground() it should display all messages with 2 seconds delay for each except last one will remain on the dialog until dialog is dismissed or hidden

@Override
protected String doInBackground(String... urls) {
    try {//this should let "Please wait " appears for 2 secs
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    publishProgress("Downloading from server");

    try {////this should let "Downloading from server" appears for 2 secs
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    publishProgress("Please wait for sometime");

    try {////this should let "Please wait for sometime" appears for 2 secs
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
     /* here i code the background downloading process*/
}

also for onProgressUpdate() the method is prepared to receive multi params, but you are sending only one, so no need to use pro[1], remove second setMessage() call

@Override
protected void onProgressUpdate(String... pro) {
    testdialog.setMessage(pro[0]);
}
Yazan
  • 6,074
  • 1
  • 19
  • 33
  • @Yazan did we calculate the network delay by coding? is it possible by the developers?, kindly need your reply. Thanks in advance – MohanRaj S Mar 10 '16 at 03:41
  • 1
    @MohanRajS well, you don't calculate network delay, if you mean how to increase progress value, then it could be the content length you are downloading, start with 0, set max= content length, and increase by downloaded bytes, or if you passed multiple links to process in `doInbackground()` you can increase progress by 1 for each url processed, max value for progress in this case is count of passed urls. other than that i am not sure if we can calculate network delay. (if i understand you correct) – Yazan Mar 10 '16 at 07:14
  • @Yazan i am receiving the JSON response in a single string, i don't know the content length. In this case can i show the progress bar loading from 0 to 100 percent . – Gokul Rajkumar Mar 14 '16 at 04:04
  • yes you can, you can also make it min 0 max 3, and make it stage-progress, ex: Downloading ... , Parsing ... , Finalizing .... so the progress will have like 3 steps, each will be about 33% of the bar size... you have many options , it's up to you how to make this – Yazan Mar 14 '16 at 07:13