14

Can i create a dialog box without negative or positive buttons. That destroys it self after specific action?

 AlertDialog.Builder dialog_detect= new AlertDialog.Builder(MainActivity.this);
 dialog.setTitle("Detecting.....");
 dialog.setMessage("Please Wait");
 dialog.show();
user2401745
  • 357
  • 2
  • 5
  • 12
  • 1
    build your own custom dialog and call `dismiss()` method when you want. reference:http://android-developers.blogspot.in/2012/05/using-dialogfragments.html – dakshbhatt21 Aug 01 '13 at 12:46
  • You can try custom dialog from [here](http://stackoverflow.com/questions/17994238/android-dialog-box-without-buttons/17994633#17994633) – Kartheek Sarabu Aug 01 '13 at 12:53
  • You can create a custom dialog: https://stackoverflow.com/questions/13341560/how-to-create-a-custom-dialog-box-in-android. – CoolMind May 27 '22 at 14:06

9 Answers9

19

You can do this very easily.

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);

// set title
alertDialogBuilder.setTitle("Your Title");

// set dialog message
alertDialogBuilder.setMessage("Message here!").setCancelable(false);

// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();

// show it
alertDialog.show();

// After some action
alertDialog.dismiss();

If you have a reference to the AlertDialog somewhere else, you can still call alertDialog.dismiss(). This closes the dialog.

uthark
  • 5,333
  • 2
  • 43
  • 59
crocboy
  • 2,887
  • 2
  • 22
  • 25
4

Really depends on what "action" is being performed:


 AlertDialog.Builder dialog_detect= new AlertDialog.Builder(MainActivity.this);
 dialog.setTitle("Detecting.....");
 dialog.setMessage("Please Wait");
 dialog.show();

 timeConsumingDetectMethod();

 dialog.dismiss();

This way you get a frozen UI until timeConsumingDetectMethod() finishes.


However, the following way runs the action in background, while a very responsive dialog is shown. Also, cancels the action when dialog is cancelled.

AsyncTask<Void,Void,Void> task = new AsyncTask<Void, Void, Void>() {

        private AlertDialog dialog;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            dialog= new AlertDialog.Builder(MainActivity.this);
            dialog.setTitle("Detecting.....");
            dialog.setMessage("Please Wait");

            dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialogInterface) {
                    cancel(true);
                }
            });

            dialog.show();
        }

        @Override
        protected Void doInBackground(Void... voids) {
            timeConsumingDetectMethod();
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            dialog.dismiss();
        }

    }.execute();
S.D.
  • 29,290
  • 3
  • 79
  • 130
3

You can try Custom Dialog design u r on Dialog and use it as u wish to use them

final Dialog dialog= new Dialog(context);
dialog.setContentView(R.layout.pre_confirmation_dailog); 
dialog.setTitle("Details...");
dialog.show();
Developer
  • 6,292
  • 19
  • 55
  • 115
3

You can also write crocboy's code like this:

AlertDialog alertDialog = new AlertDialog.Builder(context)    
    .setTitle("Your Title")
    .setMessage("Message here!")
    .setCancelable(false)
    .create();

alertDialog.show();

// After some action
alertDialog.dismiss(); 

It does exactly the same thing, it's just more compact.

LLAlive
  • 427
  • 5
  • 24
nasch
  • 5,330
  • 6
  • 31
  • 52
2

You can call alertDialog .dismiss () after any action.

KEYSAN
  • 885
  • 8
  • 23
1

to show dialog:-

ProgressDialog pd = ProgressDialog.show(context,"TITLE","MSG");

to dismiss

pd.dismiss();
r4jiv007
  • 2,974
  • 3
  • 29
  • 36
0

Implement a timer and set it to dismiss the DialogBox after a few seconds.

Guy
  • 6,414
  • 19
  • 66
  • 136
0

You can try custom dialog as you like

Dialog dialog_help = new Dialog(this);
dialog_help.setContentView(R.layout.title_multi_selectitems_dialog);
EditText et_1 = (EditText) dialog_help.findViewById(R.id.et_help_1);
dialog_help.setCancelable(true);
dialog_help.setTitle("   Help   ");
dialog_help.setCanceledOnTouchOutside(true);
dialog_help.show();
dialog_help.getWindow().setGravity(Gravity.CENTER);
dialog_help.getWindow().setLayout(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
Kartheek Sarabu
  • 3,886
  • 8
  • 33
  • 66
0

Correct code of crocboy's answer is this:

AlertDialog.Builder builder = new AlertDialog.Builder(context);

// set title
builder.setTitle("Your Title");

// set dialog message
builder.setMessage("Message here!").setCancelable(false);

// create alert dialog
AlertDialog alertDialog = builder.create();

// show it
alertDialog.show();

// After some action
alertDialog.dismiss(); 
indivisible
  • 4,892
  • 4
  • 31
  • 50
Raziel
  • 53
  • 1
  • 8