Derbie
Derbie

Reputation: 423

Dismiss AlertDialog without clicking on button

I'd like to dismiss an AlertDialog window, but not when clicking on the "ok" or "cancel" button, but when an action is called. In fact, I'd like to create the dialog in onResume function that way :

@Override
public void onResume() {
    super.onResume();
    alert = new AlertDialog.Builder(this);
    alert.setTitle("Stay stuck to a tag");
    alert.create().show();
}

and then exit it when a function is called, like

private void dismissMyDiag() {
    alert.dismiss();
}

Of course I've seen the way to do that with the onclick event by that's not what I want to do. Is it possible to do such a thing ? And if not, what king of dialog box allows me to do that ?

Thank you !

Upvotes: 3

Views: 3703

Answers (3)

Hamza Regardless
Hamza Regardless

Reputation: 83

i know it's old Question but may Hope This will help Some One. You Can also Can Create A Custom Class For Alert Dialog And Access it hole project Can Show and hide any where

package (Your_Pakage_Name);
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatDialogFragment;


public class AlertDailogbox extends AppCompatDialogFragment {

    String Title = null;
    String Msg = null;
    String TAG = "Alert Dialog Box";

    public AlertDailogbox(String title, String msg) {
        Title = title;
        Msg = msg;
        
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(Title)
                .setMessage(Msg)
                .setIcon(R.drawable.ic_alert) //set icon if you want
                .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        
                    }
                });

        return builder.create();
    }
}

Call in Your Project (Activity) Make an Global Variable of alertDialog Class

private AlertDailogbox alertDailogbox;

function to show Dialog

private void showMsgDailog(String Title,String Msg){
    alertDailogbox = new AlertDailogbox(Title,Msg);
    alertDailogbox.setCancelable(false);
    alertDailogbox.show(getSupportFragmentManager(),"alert Dailog");
}

End For Dismiss

if(alertDailogbox != null)
       alertDailogbox.dismiss();

alerDailogeBox is not null check is nessary other wise application can crash

Upvotes: 0

Dhaval Parmar
Dhaval Parmar

Reputation: 18978

try this way:

final AlertDialog d = new AlertDialog.Builder(context)
                .setView(v)
                .setTitle(R.string.my_title)
                .setPositiveButton(android.R.string.ok,
                        new Dialog.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface d, int which) {
                                //Do nothing here. We override the onclick
                            }
                        })
                .setNegativeButton(android.R.string.cancel, null)
                .create();

        d.setOnShowListener(new DialogInterface.OnShowListener() {

            @Override
            public void onShow(DialogInterface dialog) {

                Button b = d.getButton(AlertDialog.BUTTON_POSITIVE);
                b.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        // TODO Do something

                        //Dismiss once everything is OK.
                        d.dismiss();
                    }
                });
            }
        });

Upvotes: 1

Pragnani
Pragnani

Reputation: 20155

Get the alertDialog like this

AlertDialog dialog;
 alert = new AlertDialog.Builder(this);

dialog=alert.create().show();

Then call

private void dismissMyDiag() {
    dialog.dismiss();
}

Upvotes: 7

Related Questions