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”
2. Select Android build target
3. Enter application details
4. res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <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> </resources>
5. res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/tv_message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:padding="20dp" android:text="@string/str_tv_about" /> <Button android:id="@+id/btn_quit" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/str_btn_quit" /> </LinearLayout>
6. src/in/wptrafficanalyzer/openalertdialog/MyAlertDialogWindow.java
package in.wptrafficanalyzer.openalertdialog; import android.app.AlertDialog; import android.app.Dialog; import android.app.DialogFragment; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.os.Bundle; import android.widget.Toast; public class MyAlertDialogWIndow extends DialogFragment { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { /** Defining an event listener for the Yes button click */ OnClickListener positiveClick = new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getActivity().getBaseContext(), "Application is quitting ...", Toast.LENGTH_SHORT).show(); getActivity().finish(); } }; /** Defining an event listener for the No button click */ OnClickListener negativeClick = new OnClickListener() { @Override 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 */ b.setTitle("Confirmation"); /** Creating the AlertDialog , from the builder object */ Dialog d = b.create(); /** Returning the alert window */ return d; } }
7. src/in/wptrafficanalyzer/MainActivity.java
package in.wptrafficanalyzer.openalertdialog; import android.app.Activity; import android.app.FragmentManager; 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. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); /** Instantiating the quit button of the layout file */ Button quit = (Button) findViewById(R.id.btn_quit); /** Defining a click event listener for the quit button */ OnClickListener listener = new OnClickListener() { @Override public void onClick(View v) { FragmentManager fm = getFragmentManager(); /** Instantiating the DialogFragment */ MyAlertDialogWIndow alert = new MyAlertDialogWIndow(); /** Opening the dialog window */ alert.show(fm, "Alert_Dialog"); } }; /** Setting the click event listener for the button 'quit' */ quit.setOnClickListener(listener); } }
8. Application Screenshot
9. Download

10. Reference
http://developer.android.com/guide/index.html

I am George Mathew, working as software architect and Android app developer at wptrafficanalyzer.in
You can hire me on hourly basis or on project basis for Android applications development.
For hiring me, please mail your requirements to info@wptrafficanalyzer.in.
My other blogs
store4js.blogspot.com