2

I am trying to show an alert dialog which contains a loading animation. The Alert dialog needs to appear after clicking another alert dialog that was initially in display. Basically,the first dialog contains a setPositive and setNegative button. The setPositive event is triggers a process which takes a few seconds. I wanted the second dialog containing the loading animation to appear when the process begins. I am not able to dismiss the first dialog until the process is done for the loading dialog to show.

//dialog that contains loading animation public class LoadingBox { Activity myActivity; AlertDialog dialog; LoadingBox(Activity activity){ myActivity = activity; } void startLoadingDialog(){ AlertDialog.Builder builder = new AlertDialog.Builder(myActivity); LayoutInflater inflater = myActivity.getLayoutInflater(); builder.setView(inflater.inflate(R.layout.layout_dialog,null)); builder.setCancelable(true); dialog = builder.create(); dialog.show(); } void dismissDialog(){ dialog.dismiss(); } } new AlertDialog.Builder(SmsListActivity.this) .setIcon(android.R.drawable.ic_dialog_alert) .setTitle("Add To Transaction List") .setMessage("Please note that this process might take long. Proceed?") .setPositiveButton("yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { LoadingBox loadingBox = new LoadingBox(SmsListActivity.this); loadingBox.startLoadingDialog(); addPendingToList(); loadingBox.dismissDialog(); } }).setNegativeButton("No",null).show(); 

2 Answers 2

2

To dismiss this alert dialog you should call dialog.dismiss(); in setPositiveButton onClick

 new AlertDialog.Builder(SmsListActivity.this) .setIcon(android.R.drawable.ic_dialog_alert) .setTitle("Add To Transaction List") .setMessage("Please note that this process might take long. Proceed?") .setPositiveButton("yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); LoadingBox loadingBox = new LoadingBox(SmsListActivity.this); loadingBox.startLoadingDialog(); addPendingToList(); loadingBox.dismissDialog(); } }).setNegativeButton("No",null).show(); 
Sign up to request clarification or add additional context in comments.

8 Comments

when I do it like this the dialog closes but the loadingBox dialog does not appear.
You are dismissing loadingBox on the same onClick function
Where should I place the dismissing code? I thought that it should wait for addPending function to complete before it disappears
try removing loadingbox.dismissDialog line from there if still not appearing add a Handler with some delay to show the loadingbox view. you should figure out why the second alertdialog is not appearing
It appear when I remove loadingbox.dismissDialog line from click event. Now, how do I trigger it do dismiss after the process is done?
|
0

Like @MathankumarK said, calling .dismiss(); in your positive onClick function is perhaps the best option, but you could use Thread.sleep(----) after the dismiss(); to wait in milliseconds until the operation is underway and then show the "loading" dialog. Since the operation is slowing down the app thread anyway, it shouldn't pose a problem by slowing or stopping your app for a few seconds.

1 Comment

I used AsyncTask as mentioned by @MathankumarK and it handled the job very well.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.