Home > DeveloperSection > Forums > How to create AlertDialog in android?
Ankita Pandey
Ankita Pandey

Total Post:183

Points:1285
Posted on    October-16-2014 11:32 PM

 Android Android 
Ratings:


 1 Reply(s)
 597  View(s)
Rate this:
Iam Showing a AlertDialog on Device Shake. The issue is that when I shake the device the AlertDialog shows twice. Hence, if I dismiss the AlertDialog, the top one gets dismissed but there is one still showing after that behind the first one.

Also, Iam showing the same AlertDialog from another section of code where it is shown on a button click which works fine.

All of this is done within a ViewPager.

How can I avoid this scenario?

Here is my code:-

@Override
public void onShake(float force) 
{
    // TODO Auto-generated method stub
    showDialog(timeString, "Confirm .");
}
public void showDialog(String timeString, String title)
    {
        builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(title);
        builder.setMessage("Reaching at:"+" "+ timeString+"?");
        builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // Do nothing
            }
        });
        builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) 
            {
                // Do nothing but close the dialog
                new AddStatusTask().execute();
            }


Pravesh Singh

Total Post:411

Points:2881
Posted on    October-16-2014 11:36 PM

Instead of builder.show();

use

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

// show it
alertDialog.show();
and check here like

@Override
   public void onShake(float force) 
  {
// TODO Auto-generated method stub

 if(alertDialog != null && !alertDialog.isShowing()){
showDialog(timeString, "Confirm .");
} }

Don't want to miss updates? Please click the below button!

Follow MindStick