create ProgressDialog in a method - android

I have an activity. It uses nfc tag. I want to create progress dialog in method CertificateGeneration for shows steps to user. It is my code.
private void CertificateGeneration(Tag mytag)
{
ProgressDialog progress = new ProgressDialog(CertificateGenerationActivity.this);
progress.setMessage("in progress... ");
progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progress.setIndeterminate(true);
progress.show();
Method1();
progress.setMessage("Method1 is executed ");
Method2();
progress.setMessage("Method2 is executed ");}
but when I execute CertificateGeneration method in debuge mode I see all lines are parse without any changes on screen.
It does not show progress Message on screen and executes method1. why?
at the end of Method (executed Method1 and Method2) It shows progress Message. and only shows " Method2 is executed".
How can I edit my code? or It a beter solution for diplay message at run time in method(not end od method)?
I also wrote a simple method too. It does not have any method. but It is executed completly and only show "progress2"
private void DisplayMsg(final String msg)
{
progress = new ProgressDialog(CertificateGenerationActivity.this);
progress.setMessage("Progress1");
progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progress.setIndeterminate(true);
progress.show();
String f="test";
progress.setMessage("progress2");
}

If you have a heavy task to done use AsyncTask.
AsyncTask has usefull methods such as publishProgress().

Related

Progress Dialog dont move

Hi I try show progress dialog when mymethod is running with code that below. When thread starts the progress dialog stop I dismiss it at the end of the method that called in thread. How can I solve this?
>mDialog = new ProgressDialog(DealActivity.this);
mDialog.setMessage("Loading...");
mDialog.setCancelable(false);
mDialog.show();
mDialog.setProgress(0);
new Thread() {
public void run() {
mymethod();
}
}.start();
Use AsyncTask instead of thread. Show your ProgressDialog in onPreExecute() method call your mymethod() in doInBackGround() and dismiss you progressDialog in onPostExecute().
Follow this tutorial for better explanation.
Android Threads, Handlers and AsyncTask
You'll also find this tutorial related to your need.
Managing AsyncTask, progress dialog and screen orientation

changing Message for ProgressDialog android

I have an ProgressDialog where in shows while sending mail. The progress dialog works across activities and classes as one of the blog had given an hint showing ProgressDialog across activities.
Below is the code as I am overriding onCreateDialog()
#Override
protected Dialog onCreateDialog(int id) {
if(id == ID_SENDING_MAIL){
ProgressDialog loadingDialog = new ProgressDialog(this);
loadingDialog.setMessage("Sending Email...");
loadingDialog.setIndeterminate(true);
loadingDialog.setCancelable(true);
return loadingDialog;
}
return super.onCreateDialog(id);
}
then I call the mail sending as below
showDialog(ID_SENDING_MAIL);
new Thread(new Runnable(){
public void run(){//I am calling Mail Send here
dismissDialog(Email.ID_SENDING_MAIL);
}
}).start();
In run method I instantiate mail class and send host of parameters.
This is working fully but I want to set different messages to ProgressDialog.
Like at the time of connecting to Host
Sending Mail then
Mail Sent Successfully
How could we carry out those changes when used with onCreateDialog().
Looking forward to your reply.
thanks.
best way to do this by using AsyncTask:
and in onProgressUpdate(Integer... progress) use progress param to set desired messages to ProgressDialog(using swith of any other method to determine wat the exact message should be)
If you want to display different messages on different stages in network thread, then use AysncTask, and after each step in doInBackground() method, invoke publishProgress method.
in onProgressUpdate(Integer... progress) method change message in progress dialog.

Progress bar on a button click

In my app, at the click of a button, the app does some processing, sends some data over the network and then stops. As it takes some time, I tried putting in a progress bar. The code for the progress bar is at the beginning of the onclick listener for the button. After the progress bar code, the processing and sending data over the network takes place.
But the progress bar is not visible at all. Do I need to necessarily show the progress bar in a separate thread?
This is what i have used to show the progress bar
final ProgressDialog pd=new ProgressDialog(this);
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pd.setMessage("Please wait..");
pd.setCancelable(true);
pd.setIndeterminate(true);
pd.show();
use the AsyncTask class which was used to do process in background and you can also showing up your progressbar there
Here is the simple snippet of code for AsyncTask
class BackgroundProcess extends AsyncTask<Void,Void,Void>{
private ProgressDialog progress;
public doInBackground(Void...arg){
publishProgress();
// do your processing here like sending data or downloading etc.
}
#Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
progress = ProgressDialog.show(YourActivity.this, "", "Wait...");
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if(progress!=null)
progress.dismiss();
progress = null;
}
}
now initialize and execute it in button onclick listener like this way
new BackgroundProcess().execute();
now progressdialog will be publish and appear on the screen and when process was completed then from the onPostExecute() just dismiss the progress dialog
I pasted your code into my application, and it works just fine. Are you calling all of that from the UI Thread? If you are doing some heavy processing and data transmission, make sure not to run that on the UI thread. Make an AsyncTask to handle the network stuff.
EDIT: I moved it into its own thread, and it no longer works, so make sure it's being called from the UI Thread.

progress dialog not showing in android?

when does the progress dialog not show in android? i want to know the circumstances when the above can happen:
in my case the progress dialog was not showing in this case:
func{
progressdialog.show();
....
.....
anotherfunction();
listview.setAdapter();
progressdialog.dismiss();
}
what is the general rule of thumb with dialog boxes?
thank you in advance.
EDIT
when the .show() command is executed the progress dialog should show. But when the otherfucntion() is called, does the previous command of progressdialog show stop?
Seems like you need to use AsyncTask the UI (including the progressDialog) will not update if the UI thread is still busy. There are many examples in SO for that.
And as a rule of thumb - if you need Progress dialog - you need AsyncTask.
It is not that any command stops, it is just that if you execute a sequence of methods on the UI thread, the UI will probably not be updated until the sequence is over, which is after progressDialog.dismiss(), so the progressDialog should not be displayed anymore.
I think You have to do this in your activity.
ProgressDialog _progressDialog = ProgressDialog.show(this,"Saving Data","Please wait......");
settintAdater();
private void settingAdater(){
Thread _thread = new Thread(){
public void run() {
Message _msg = new Message();
_msg.what = 1;
// Do your task where you want to rerieve data to set in adapet
YourCalss.this._handle.sendMessage(_msg);
};
};
_thread.start();
}
Handler _handle = new Handler(){
public void handleMessage(Message msg) {
switch(msg.what){
case 1:
_progressDialog.dismiss();
listview.setAdapter();
}
}
}
To show a ProgressDialog use
ProgressDialog progressDialog = ProgressDialog.show(PrintMain.this, "",
"Uploading Document. Please wait...", true);
And when you have completed your task use
progressDialog.dismiss();
to dismiss the ProgressDialog ..
You can call to show the ProgressDialog in your onPreExecute method of AsyncTask class and when your done dismiss it in the onPostExecute method

Updating progress dialog in Activity from AsyncTask

In my app I am doing some intense work in AsyncTask as suggested by Android tutorials and showing a ProgressDialog in my main my activity:
dialog = ProgressDialog.show(MyActivity.this, "title", "text");
new MyTask().execute(request);
where then later in MyTask I post results back to activity:
class MyTask extends AsyncTask<Request, Void, Result> {
#Override protected Result doInBackground(Request... params) {
// do some intense work here and return result
}
#Override protected void onPostExecute(Result res) {
postResult(res);
}
}
and on result posting, in main activity I hide the dialog:
protected void postResult( Result res ) {
dialog.dismiss();
// do something more here with result...
}
So everything is working fine here, but I would like to somehow to update the progress dialog to able to show the user some real progress instead just of dummy "Please wait..." message. Can I somehow access the progress dialog from MyTask.doInBackground, where all work is done?
As I understand it is running as separate Thread, so I cannot "talk" to main activity from there and that is why I use onPostExecute to push the result back to it. But the problem is that onPostExecute is called only when all work is already done and I would like to update progress the dialog in the middle of doing something.
Any tips how to do this?
AsyncTask has method onProgressUpdate(Integer...) that you can call each iteration for example or each time a progress is done during doInBackground() by calling publishProgress().
Refer to the docs for more details
you can update from AsyncTask's method onProgressUpdate(YOUR_PROGRESS) that can be invoked from doInBackground method by calling publishProgress(YOUR_PROGRESS)
the data type of YOUR_PROGRESS can be defined from AsyncTask<Int, YOUR_PROGRESS_DATA_TYPE, Long>

Categories

Resources