Opening an Alert Dialog Window in Android

July 5, 2012

Alert dialog window is a small window which can float on the top of an activity. It can either show some text messages or complex views by customising its layout.

In this article we will create an application which will use the alert dialog window for user confirmation. You can see the screen shot of this application towards the end of this article.

Note : This application is developed in Eclipse 3.7.2

1. Create and Android project namely “OpenAlertDialog”

New Android Project

Figure 1 : New Android Project

2. Select Android build target

Select Android Build Target

Figure 2 : Select Android Build Target

3. Enter application details

Enter application Details

Figure 3: Enter application Details

4. res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?>

    <string name="hello">Hello World, MainActivity!</string>
    <string name="app_name">OpenAlertDialog</string>
    <string name="str_tv_about">An Alert Dialog Demo</string>
    <string name="str_btn_quit">Quit</string>


5. res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    android:orientation="vertical" >


        android:text="@string/str_btn_quit" />


6. src/in/wptrafficanalyzer/openalertdialog/

package in.wptrafficanalyzer.openalertdialog;

import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.widget.Toast;

public class MyAlertDialogWIndow extends DialogFragment {

    public Dialog onCreateDialog(Bundle savedInstanceState) {

        /** Defining an event listener for the Yes button click */
        OnClickListener positiveClick = new OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getActivity().getBaseContext(), "Application is quitting ...", Toast.LENGTH_SHORT).show();

        /** Defining an event listener for the No button click */
        OnClickListener negativeClick = new OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getActivity().getBaseContext(), "Returning to the main activity", Toast.LENGTH_SHORT).show();


        /** Creating a builder object for the AlertDialog*/
        AlertDialog.Builder b = new AlertDialog.Builder(getActivity());

        /**  Setting the message */
        b.setMessage("Do you want to quit this application ?");

        /** Setting the Negative button */
        b.setNegativeButton("No", negativeClick);

        /** Setting the Positive button */
        b.setPositiveButton("Yes", positiveClick);

        /** Setting a title for the dialog */

        /** Creating the AlertDialog , from the builder object */
        Dialog d = b.create();

        /** Returning the alert window */
        return d;


7. src/in/wptrafficanalyzer/

package in.wptrafficanalyzer.openalertdialog;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity{
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {

        /** Instantiating the quit button of the layout file */
        Button quit = (Button) findViewById(;

        /** Defining a click event  listener for the quit button */
        OnClickListener listener = new OnClickListener() {
            public void onClick(View v) {
                FragmentManager fm = getFragmentManager();

                /** Instantiating the DialogFragment */
                MyAlertDialogWIndow alert = new MyAlertDialogWIndow();

                /** Opening the dialog window */
      , "Alert_Dialog");


        /** Setting the click event listener for the button 'quit' */

8. Application Screenshot

Application Screenshot

Figure 4: Application Screenshot

9. Download

10. Reference

How to hire me?

I am George Mathew, working as software architect and Android app developer at

You can hire me on hourly basis or on project basis for Android applications development.

For hiring me, please mail your requirements to

My other blogs

Android Knowledge Quiz

Ready to test your knowledge in Android? Take this quiz :

Tags: , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

Be friend at g+

Subscribe for Lastest Updates

FBFPowered by ®Google Feedburner