简体   繁体   中英

How can I open and auto-dismiss an Alert Dialog when opening a new section?

I am trying to have a dialog pop up on screen when case 2 is selected from the Action bar, preferably without clicking on a button. As of right now, I can only get it to work onClick and not when the page opens. How can I get rid of the button and only have it show on open for a few seconds, then dismiss? Thanks.

case 2:
                // get button
                Button btnShow = (Button)findViewById(R.id.btn_show);
                btnShow.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
                        builder.setTitle("Auto-closing Dialog");
                        builder.setMessage("After 2 second, this dialog will be closed automatically!");
                        builder.setCancelable(true);

                        final AlertDialog dlg = builder.create();

                        dlg.show();

                        final Timer t = new Timer();
                        t.schedule(new TimerTask() {
                            public void run() {
                                dlg.dismiss(); // when the task active then close the dialog
                                t.cancel(); // also just top the timer thread, otherwise, you may receive a crash report
                            }
                        }, 2000); // after 2 second (or 2000 miliseconds), the task will be active.

                    }
                });

You can use Handle for your requirement :

case 2:
                // get button
                Button btnShow = (Button)findViewById(R.id.btn_show);
                btnShow.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
                        builder.setTitle("Auto-closing Dialog");
                        builder.setMessage("After 2 second, this dialog will be closed automatically!");
                        builder.setCancelable(true);

                        final AlertDialog dlg = builder.create();

                        dlg.show();

                        Handler mHandler = new handler();
            Runnable mRunnable = new Runnable () {

                public void run() {
                    if(dlg != null && dlg.isShowing()) dlg.dismiss();
                }
            };
            mHandler.postDelayed(mRunnable,2000);

                    }
                });

UPDATE :

I have updated code for removing errors

This code will dismiss the alert dialog after 2 seconds user clicks on button. Is this what you want to do ?

If you want to hide the Button and want to have click functionalty use :

Button btnShow = (Button)findViewById(R.id.btn_show);
btnShow.setVisibility(View.GONE);
btnShow.performClick();

I really do not understand what you are trying to say.Your following statement is making confusion

I am trying to have a dialog pop up on screen when case 2 is selected from the Activity bar,

I do not know what do you meant by case 2 and activity bar. but in case if it is actionbar and you are using spinner in it then you can override the following method

android.support.v7.app.ActionBar.OnNavigationListener navigationListener = new android.support.v7.app.ActionBar.OnNavigationListener() {

            @Override
            public boolean onNavigationItemSelected(int itemPosition, long itemId) {
                //Here you can implement the case 

so when you are able to show the dialog box, You can start timer with the start of dialog box and you can then on finish of timer you can close the dialog box.

I hope this make sense to you. Please reply back if you need help I can provide source code.

ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
      Runnable hideDialog = new Runnable() {
          public void run() {
              dialog.dismiss();
         }
    };
    executor.schedule(hideDialog, 2, TimeUnit.SECONDS);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM