I want to open a ProgressDialog when I click on the List Item that opens the data of the clicked Item form the Web Service.
The ProgressDialog needs to be appeared till the WebContent of the clicked Item gets opened.
I know the code of using the Progress Dialog but I don't know how to dismiss it particularly.
I have heard that Handler is to be used for dismissing the Progress Dialog but I didn't found any worth example for using the Handler ultimately.
Can anybody please tell me how can I use the Handler to dismiss the Progress Dialog?
Thanks,
david
Hi this is what you want
public void onClick(View v)
{
mDialog = new ProgressDialog(Home.this);
mDialog.setMessage("Please wait...");
mDialog.setCancelable(false);
mDialog.show();
new Thread(new Runnable()
{
#Override
public void run()
{
statusInquiry();
}
}).start();
}
here is the web webservice that is called
void statusInquiry()
{
try
{
//calling webservice
// after then of whole web part you will send handler a msg
mHandler.sendEmptyMessage(10);
}
catch (Exception e)
{
mHandler.sendEmptyMessage(1);
}
}
and here goes handler code
Handler mHandler = new Handler()
{
public void handleMessage(android.os.Message msg)
{
super.handleMessage(msg);
switch (msg.what)
{
case 10:
mDialog.dismiss();
break;
}
}
}
};
A solutiion could be this:
ProgressDialog progressDialog = null;
// ...
progressDialog = ProgressDialog.show(this, "Please wait...", true);
new Thread() {
public void run() {
try{
// Grab your data
} catch (Exception e) { }
// When grabbing data is finish: Dismiss your Dialog
progressDialog.dismiss();
}
}.start();
Related
I'm learning android and I don't know why this code doesn't work. Can you tell me why it doesn't work and take me correct code?
final ProgressDialog dialog = ProgressDialog.show(LoginScreen.this, "", "Loading. Please wait...", true);
Thread loggingStatus = new Thread() {
public void run() {
try
{
sleep(2000);
dialog.setMessage("Logging in. Please wait.");
sleep(2000);
dialog.dismiss();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
};
loggingStatus.start();
You have to move the portion of the background task that updates the ui onto the main thread. There is a simple piece of code for this:
putting runOnUiThread( new Runnable(){ .. inside run():
final ProgressDialog dialog = ProgressDialog.show(this, "", "Loading. Please wait...", true);
Thread loggingStatus = new Thread() {
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
try {
sleep(2000);
dialog.setMessage("Logging in. Please wait.");
sleep(2000);
dialog.dismiss();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
};
loggingStatus.start();
You should not set the Text of ProgressDialog in background thread . All UI updation should be done only on UI thread(Main thread)
Either use AsyncTask or Handler for this logic or functionality
Im Adding Progress Dialog in some Activity .But im getting Exception mention in title.how to resolve it.
dialog = ProgressDialog.show(Notification.this, "loading please wait",
"Loading. Please wait...", true);
new Thread() {
public void run() {
try{
performBackgroundProcess1();
//sleep(3000,000);
} catch (Exception e) {
Log.e("tag", e.getMessage());
}
// dismiss the progress dialog
dialog.dismiss();
}
}.start();
Any thing wrong with this.all Background process is performed in performbackgroundprocess method.
You cant call dialog.dismiss(); in the background thread.
You can make Threads send messages to handlers when they are done and in the handler you can dismiss the dialog. Handlers work in ui thread
There is a tutorial about it
use runOnUiThread as:
new Thread() {
public void run() {
try{
performBackgroundProcess1();
//sleep(3000,000);
} catch (Exception e) {
Log.e("tag", e.getMessage());
}
// dismiss the progress dialog
CurrentActivity.this.runOnUiThread(new Runnable(){
#Override
public void run() {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
}
}.start();
Could someone tell me why my code is not working? I want to display a ProgressDialog, but the problem with the code below is that it does not appear, even when it has spent a lot of time processing the function ConsultaComercio. I have seen a lot of examples but I don't understand what I am doing wrong. I appreciate your help. Thanks in advance.
pd = ProgressDialog.show(this, "", "Loading...", true);
Toast.makeText(getApplicationContext(), "Cargando.... " + String.valueOf(numero_prueba), Toast.LENGTH_SHORT).show();
new Thread() {
public void run() {
try{
// Do some Fake-Work
ConsultaComercio();
numero_prueba=60000;
} catch (Exception e) { }
// Dismiss the Dialog
pd.dismiss();
}
}.start();
in your onCreate() do this,
Handler handler=new Handler()
{
public void handleMessage(Message msg)
{
if(pd.isShowing())
{
pd.dismiss();
}
};
and change your thread like this,
Toast.makeText(getApplicationContext(), "Cargando.... " + String.valueOf(numero_prueba), Toast.LENGTH_SHORT).show();
new Thread() {
public void run() {
try{
// Do some Fake-Work
ConsultaComercio();
numero_prueba=60000;
} catch (Exception e) { }
// Dismiss the Dialog
handler.sendEmptyMessage(0);
}
}.start();
You can't update the UI from just any thread. It must be an AsyncTask.
Here is my code segment I am trying to dismiss dialog but it is not getting dismissed and also i don't get any error on logcat.Please correct me where i am wrong ?
All the Log.v statement get executed.Even log.v statement after pd.dismiss() (Log.v("TAG","progress dismiss");) gets printed.
Please point my mistake or suggest some alternative way to dismiss the progressdialog.
btnSave.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//do something
} else {
Log.v("TAG","above progressDialog");
final ProgressDialog pd = new ProgressDialog(ChangePassword.this);
ProgressDialog.show(ChangePassword.this, "", "Loading...", false, true);
new Thread() {
public void run() {
try {
sleep(2000);
Log.v("TAG","in try block");
} catch (Exception e) {
Log.e("tag", e.getMessage());
}
// dismiss the progress dialog
pd.dismiss();
// Log.v("TAG","progress dismiss");
}
}.start();
Log.v("TAG","after start");
public void run()
{
try{
if (!(txtOldPass.getText().toString())
.equals(SetGetValues.getPassword())) {
Toast.makeText(ChangePassword.this,
"Invalid Old Password", Toast.LENGTH_SHORT)
.show();
txtOldPass.setText("");
txtNewPass.setText("");
txtCnfPass.setText("");
} else {
if (!(txtNewPass.getText().toString())
.equals(txtCnfPass.getText().toString())) {
Toast.makeText(ChangePassword.this,
"Re-Enter New Password",
Toast.LENGTH_SHORT).show();
txtOldPass.setText("");
txtNewPass.setText("");
txtCnfPass.setText("");
} else {
try {
handler = new Handler();
handler.postDelayed(new Thread (new Runnable(){
JSONStringer loginuser = new JSONStringer()
.object()
.key("userid")
.value(SetGetValues.getUserid()
.trim())
.key("password")
.value(txtCnfPass.getText()
.toString().trim())
.key("oldpassword")
.value(txtOldPass.getText()
.toString().trim())
.endObject();
StringEntity entity = new StringEntity(
loginuser.toString());
JSONObject results = bc
.returnJSONObject(loginuser,
"url");
String message = results
.getString("message");
String isvalid = results
.getString("isvalid");
if (isvalid.contains("FALSE")) {
} else {
//pd.dismiss();
Toast.makeText(
ChangePassword.this,
"Password changed successfully",
Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
//TODO: handle exception
e.printStackTrace();
}
}
}
}catch(Exception e)
{
e.printStackTrace();
}
}
}), 1000);
}
}
});
You are not dismissing the same ProgressDialog that you are showing. Replace this:
final ProgressDialog pd = new ProgressDialog(ChangePassword.this);
ProgressDialog.show(ChangePassword.this, "", "Loading...", false, true);
with this:
final ProgressDialog pd = ProgressDialog.show(ChangePassword.this, "", "Loading...", false, true);
if (pd.isShowing()) {
pd.dismiss();
}
Instead of using thread Use Async Task
usage
You problem probably is, that you're trying to modify the UI (dismiss the dialog) from another thread. I'm surprised that you don't get an error message. Try dismissing the dialog from the UI thread, for example by using .runOnUIThread().
Because you are trying to use element of UI Thread progressDialog in a non UI thread .
This will be allowed through AsyncTask , handler or RunOnUIThread(runnable) only .
read more about them and use any one .
I have a method in my activity to download a set of files. This downloading is taking place when I start a new activity. I have used threads, because it downloads completely whereas AsyncTask may sometimes fail to download all files, it may get stuck in between.
Now, a black screen is shown when the downloading takes place. I want to show it within a ProgressDialog so that user may feel that something is getting downloaded.
I have added a ProgressDialog, but its not showing. Can anyone tell where did I go wrong?
Below is my code:
Inside onCreate() I have written:
downloadFiles();
private boolean downloadFiles() {
showProgressDialog();
for(int i = 0; i < filesList.size();i++) {
Thread thread = new Thread(new Runnable() {
#Override
public void run() {
//downloading code
});
thread.start();
thread.run();
}
dismissProgressDialog();
return true;
}
//ProgressDialog progressDialog; I have declared earlier.
private void showProgressDialog() {
progressDialog = new ProgressDialog(N12ReadScreenActivity.this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setMessage("Downloading files...");
progressDialog.show();
}
private void dismissProgressDialog() {
if(progressDialog != null)
progressDialog.dismiss();
}
Try this .. it's simple
ProgressDialog progress = new ProgressDialog(this);
progress.Indeterminate = true;
progress.SetProgressStyle(ProgressDialogStyle.Spinner);
progress.SetMessage("Downloading Files...");
progress.SetCancelable(false);
RunOnUiThread(() =>
{
progress.Show();
});
Task.Run(()=>
//downloading code here...
).ContinueWith(Result=>RunOnUiThread(()=>progress.Hide()));
Please try Below Code .
private Handler responseHandler=null;
downloadFiles();
private boolean downloadFiles() {
showProgressDialog();
for(int i = 0; i < filesList.size();i++) {
Thread thread = new Thread(new Runnable() {
#Override
public void run() {
//downloading code
responseHandler.sendEmptyMessage(0);
});
thread.start();
}
responseHandler = new Handler()
{
public void handleMessage(Message msg)
{
super.handleMessage(msg);
try
{
dismissProgressDialog()
}
catch (Exception e)
{
e.printStackTrace();
}
}
};
}
Here in this code when ever your dowload will completed it called response handler and your progress dialog will dismiss.
In downloadFiles() you show the dialog, then start a number of threads and after they've been started the dialog got dismissed. I don't think this is what you want as the dialog gets closed right after the last thread is started and not after the last thread has finished.
The dismissProgressDialog() method must be called after the last thread has finished its work. So at the end of the code run in the thread you have to check whether other threads are still running or whether you can dismiss the dialog as no other threads are running.
Try the following code and let me know how it goes:
private Handler mHandler = new Handler(){
public void handleMessage(Message msg)
{
dismissProgressDialog()
}
};
private boolean downloadFiles() {
showProgressDialog();
for(int i = 0; i < filesList.size();i++) {
Thread thread = new Thread(new Runnable() {
#Override
public void run() {
//downloading code
});
thread.start();
thread.run();
}
mHandler.sendEmptyMessage(0);
return true;
}
//ProgressDialog progressDialog; I have declared earlier.
private void showProgressDialog() {
progressDialog = new ProgressDialog(N12ReadScreenActivity.this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setMessage("Downloading files...");
progressDialog.show();
}
private void dismissProgressDialog() {
if(progressDialog != null)
progressDialog.dismiss();
}