Home > front end >  AlertDialog android studio
AlertDialog android studio

Time:03-16

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();

CodePudding user response:

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();

CodePudding user response:

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.

CodePudding user response:

On your positive button's click listener calling dialog.dismiss(); is the best that I can think of! I see other people is suggesting the same thing here. Have you tried this? Are you having any issues after taking this approach?

  • Related