Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close an Android Material Alert Dialog Programatically?

I have a dialog that appears whenever some background process is going on and displays a loading ProgressBar. I am however unable to dismiss it as the dismiss() function does not work. In my code below in the hideLoading() function is supposed to close the dialog but .dismiss() doesn't work. Kindly assist.

MainActivity.Java

public class LoginActivity extends AppCompatActivity {
MaterialAlertDialogBuilder progress;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    progress = new MaterialAlertDialogBuilder(LoginActivity.this);

    login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (password_text.getText().length() < 5) {
                password_layout.setError(getResources().getString(R.string.valid_short_password));
            } else {
                showLoading();
            }
        }
    });
}

private void showLoading() {
    progress.setView(R.layout.loading);
    progress.setCancelable(false);
    progress.show();
}

private void hideLoading() {
    progress.dismiss();
}
}
like image 766
Biko Avatar asked Oct 24 '25 00:10

Biko


2 Answers

In your code, "progress" is not a dialog instance, it's a MaterialDialogBuider instance. You should get an instance of AlertDialog by something like this.

MaterialAlertDialogBuilder builer = new MaterialAlertDialogBuilder(MainActivity.this);
builer.setView(R.layout.loading);
builer.setCancelable(false);
AlertDialog dialog = builder.show();

In order to dismiss the dialog, you can call dismiss function like this.

dialog.dismiss();
like image 177
Kevin Avatar answered Oct 26 '25 14:10

Kevin


Your progress object is an MaterialAlertDialogBuilder, not an AlertDialog itself. You can't call dismiss on the builder. First get an AlertDialog object from the builder via progress.create(), then show it. After that, you'll also be able to dismiss it. E.g.

public class LoginActivity extends AppCompatActivity {
MaterialAlertDialogBuilder progress;
AlertDialog progressDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    progress = new MaterialAlertDialogBuilder(LoginActivity.this);

    login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (password_text.getText().length() < 5) {
                password_layout.setError(getResources().getString(R.string.valid_short_password));
            } else {
                showLoading();
            }
        }
    });
}

private void showLoading() {
    progress.setView(R.layout.loading);
    progress.setCancelable(false);
    progressDialog = progress.create();
    progressDialog.show();
}

private void hideLoading() {
    progressDialog.dismiss();
}
}

Note: I'm not sure how cancelable property works on the alert dialog, but you might also allow it to be cancelable to be able to dismiss it. If the above code doesn't work, try adding progress.setCancelable(true).

like image 39
chef417 Avatar answered Oct 26 '25 16:10

chef417