Biko
Biko

Reputation: 342

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

Upvotes: 4

Views: 7360

Answers (2)

Adrian Rusin
Adrian Rusin

Reputation: 635

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

Upvotes: 10

chef417
chef417

Reputation: 544

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).

Upvotes: 3

Related Questions