Thursday 17 May 2012

Integrate Progress Dialog Box in android

To integrate progress dialog in android application, it shows dialog box and processing particular tasks in background. following is the code...njoy...


There are four processes in Progress dialog - 
1. onPreExecute - shows loading view to user, he cant interacts with screen.
2. doInBackground - all the processes runs in background of Loading screen of onPreExecute.
3. onProgressUpdate and - set the current progress of the progress dialog
4. onPostExecute - must contain  progressDialog.dismiss(); so that user can interacts with screen. if you want to set dynamic data which came from doInBackground method  you can put it here. so user can see it. 


Put following class in your main activity class. after completion of onCreate(). and call this in onCreate() or wherever you need to show dialog box and run process in background.


To call this class use ----->> new LoadViewTask().execute();



private class LoadViewTask extends AsyncTask<Void, Integer, Void> {
private static final int length = 0;


// Before running code in the separate thread
@Override
protected void onPreExecute() {
progressDialog = ProgressDialog.show(CheckWeb_Status.this,
"Loading...", "Loading, please wait...", false, false);


}


// The code to be executed in a background thread.
@Override
protected Void doInBackground(Void... params) {


//USER DEFINED PROCESSES THAT RUNS IN BACKGROUND ON PROGRESS DIALOG
                     // PROCESS-1
ParseJson(data);
ParseXml();


/*
* This is just a code that delays the thread execution 4 times,
* during 850 milliseconds and updates the current progress. This is
* where the code that is going to be executed on a background
* thread must be placed.
*/
try {
// Get the current thread's token
synchronized (this) {
// Initialize an integer (that will act as a counter) to
// zero
int counter = 0;
// While the counter is smaller than four
while (counter <= 4) {
// Wait 850 milliseconds
this.wait(850);
// Increment the counter
counter++;
// Set the current progress.
// This value is going to be passed to the
// onProgressUpdate() method.
publishProgress(counter * 25);
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;


}


// Update the progress
@Override
protected void onProgressUpdate(Integer... values) {
// set the current progress of the progress dialog
progressDialog.setProgress(values[0]);
}


// after executing the code in the thread
@Override
protected void onPostExecute(Void result) {


// close the progress dialog
Log.v("Progress", "Entered");




                        //SET DATA CAME FROM doInBackground() PARSING
try {


if (json.getString(TAG_UMRP).toString().equals("")) {
tv_umrp.setText("MRnk : No MozRank found");
} else {
tv_umrp.setText(" MRnk  \t\t: " + json.getString(TAG_UMRP));
}


if (json.getString(TAG_UPA).toString().equals("")) {
tv_upa.setText("Page Authority : No Data found");
} else {
tv_upa.setText("Page Authority : "
+ json.getString(TAG_UPA));
}


if (json.getString(TAG_UPL).toString().equals("")) {
tv_upl.setText("Root Domain : No Root Domain found");
} else {
tv_upl.setText("Root Domain : " + json.getString(TAG_UPL));
}


Log.v("Progress", "Setted");


// THIS IS MUST progressDialog.dismiss();
progressDialog.dismiss();


} catch (Exception e) {


}
                  //SET DATA CAME FROM doInBackground() PARSING
tv_PageAlx.setText("Page Alexa \t\t: "
+ String.valueOf(AlexaData).replace("[", "")
.replace("]", ""));
System.out.println("Page alexa " + String.valueOf(AlexaData));
tv_PageRnk.setText("Page Rank \t\t: "
+ String.valueOf(RankData).replace("[", "")
.replace("]", ""));
System.out.println("Page Rank " + String.valueOf(RankData));
/*
* tv_CompeteRnk .setText("Compete Rank : " +
* String.valueOf(CompeteData).replace("[", "") .replace("]", ""));
*/
Log.d("Parsing", "Page Rank : " + RankData);
Log.d("Parsing", "Page Alexa : " + String.valueOf(AlexaData));


SendEmail(); // PROCESS RUNS IN BACKGROUND
}


}




2 comments:

Jaseel AV said...

Sir.
I am beginner in android.
I am using android 2.2.
I tried alot.
But won't work for me
Is it a prblm?
If you don't mind please forward me that project source code.
Anyone help me.
Please

Jaseel AV said...

Sir my email id is
=jasi3994@gmail.com
anyone help me. Please