

AlertDialog is used to show pop up alert to the user in a Dialog box. In this tutorial we are going to show you how to implement AlertDialog in Android with a example.
Creating Project:
Make sure you have properly setup the Android SDK, AVD for Testing the Application. Create a New project in Eclipse IDE with the package as “learn2crack.alertdialog”. Create the Main Activity as “MainActivity” and the main Layout as “activity_main”.
Download Complete Project:
Creating Layout:
The Main layout for our project is “activity_main” which has a button to show the AlertDialog.
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/showbtn" android:text="Show Dialog"/> </LinearLayout>
Creating Activity:
In our MainActivity “AlertDialog.Builder” is used to create a new AlertDialog. Here “builder” is the new AlertDialog interface. “setTitle” is used to assign title of the pop up dialog interface, “setMessage” is used to set a custom message in the Dialog.The “setPositiveButton”, “setNegativeButton”, “setNeutralButton” are used to add button to the Dialog. On button click here I used Toast to show a message.
MainActivity.java
package learn2crack.alertdialog; import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { Button Show; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Show = (Button)findViewById(R.id.showbtn); Show.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // TODO Auto-generated method stub AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("Alert Dialog"); builder.setMessage("This is Example of Alert Dialog with three Buttons"); builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), "Ok is clicked", Toast.LENGTH_LONG).show(); } }); builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), "Cancel is clicked", Toast.LENGTH_LONG).show(); } }); builder.setNeutralButton("Close", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), "Close is clicked", Toast.LENGTH_LONG).show(); } }); builder.show(); //To show the AlertDialog } }); } }
Creating Manifest:
No other special Permissions are required for our project.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="learn2crack.alertdialog" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="learn2crack.alertdialog.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Finally run the project in the Emulator.
Enjoy 🙂
Any questions comment here.