'Custom alert dialog for loading animation is not showing during code execution
I have several methods that uses API's in order to take stuff, and until they are done I wanted to put a loading animation so the user knows that the app is currently loading. I created a custom loading dialog which is working okay, but I noticed that the dialog is showed after the code is executed and not while the code is executing which is the case I want.
public class LoadingDialog {
private Context context;
private Dialog dialog;
LoadingDialog(Context myContext) {
context = myContext;
}
void startLoadingAnimation() {
dialog=new Dialog(context);
dialog.setContentView(R.layout.custom_loading_dialog);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.create();
dialog.show();
}
// dismiss method
void dismissLoadingAnimation() {
dialog.dismiss();
}
}
This is the class of the custom loading animation.
loadingDialog.startLoadingAnimation();
// some code here will be executed.
loadingDialog.dismissLoadingAnimation();
This is the way I want to use it. But it only shows the dialog(if i do not use the dismissLoadingAnimation) after the code is executed and I want it to show it while the code is executing and when its done, to be dismissed.
Is there a way to implement this and not change my class coding?
Solution 1:[1]
Try to remove dialog.create() and add dialog.setCancelable(false) for prevent dialog cancel when user try to click the screen after the dialog show.
add dialog.setCancelable(false) before dialog.show().
This is my code (kotlin)
var progress = Dialog(this)
progress.window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
progress.setContentView(R.layout.layout_loading)
progress.setCancelable(false)
progress.show()
and this code to stop the dialog (kotlin)
progress.dismiss()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jonry Simbolon |
