---
title: "How to create AlertDialog in android?"  
description: "How to create AlertDialog in android?"  
author: "Anonymous User"  
published: 2014-10-16  
updated: 2014-10-16  
canonical: https://www.mindstick.com/forum/2396/how-to-create-alertdialog-in-android  
category: "android"  
tags: ["android"]  
reading_time: 2 minutes  

---

# How to create AlertDialog in android?

Iam Showing a AlertDialog on [Device](https://www.mindstick.com/blog/149/retriving-network-device-information-in-c-sharp) Shake. The issue is that when I shake the device the AlertDialog [shows](https://yourviews.mindstick.com/view/81380/new-york-violent-shooting-shows-people-are-not-safe-in-night-life) 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](https://yourviews.mindstick.com/view/297/reservation-for-economically-weaker-section) of [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) where it is shown on a [button click](https://www.mindstick.com/forum/790/form-gets-submiited-twice-on-button-click) which [works fine](https://answers.mindstick.com/qa/115732/my-software-works-fine-on-windows-10-but-crashes-on-windows-11-why).\
All of this is done within a ViewPager.\
How can I [avoid](https://yourviews.mindstick.com/story/1518/tips-to-avoid-dengue-at-home) this [scenario](https://yourviews.mindstick.com/view/81221/sports-will-change-the-education-scenario)?\
**Here is my code:-**\

```
@Overridepublic 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();            }
```

## Replies

### Reply by Pravesh Singh

Instead of builder.show();use\

```
// create alert dialogalertDialog = builder.create();// show italertDialog.show();and check here like@Override   public void onShake(float force)   {// TODO Auto-generated method stub if(alertDialog != null && !alertDialog.isShowing()){showDialog(timeString, "Confirm .");} }
```


---

Original Source: https://www.mindstick.com/forum/2396/how-to-create-alertdialog-in-android

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
