Home > Net >  How to show a dialog in Android at the begin of the method, when it is called?
How to show a dialog in Android at the begin of the method, when it is called?

Time:12-24

I am trying to show a dialog while my method is executing. The problem is that the dialog shows when the method end is reached. How is it possible to show it, as soon as I call loadingDialog.show() ?

    public void test(View v){
    loadingDialog = new LoadingDialog(this);
    loadingDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

    loadingDialog.show();

    //......
    }

CodePudding user response:

If you want to do that, you need to either launch a thread or a Kotlin coroutine so the actual work can be done in the background.

CodePudding user response:

Did you tried putting the dialog's code in another method and call that method at the beggining of the test method? Like:

private void callDialog(){
loadingDialog = new LoadingDialog(this);
loadingDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

loadingDialog.show();
}

public void test(View v){
callDialog();
//......
}
  • Related