Alert Dialog – Android Example

person folder_openAndroidaccess_time February 28, 2015

In this tutorial we are going to show how to use default AlertDialog.

See flowing Steps :

  1. Use  AlertDialog.Builder
  2. Set title, message and icon

1. Manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.alertdialogandroid"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.alertdialogandroid.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>

2. Activity layout file

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.alertdialogandroid.MainActivity">
    <TextView android:id="@+id/infotext"
        android:layout_marginTop="10dp"
        android:text="Click the button to see how alert dialog works.."
        android:layout_height="wrap_content"
        android:layout_width="match_parent"/>
    <Button android:id="@+id/alertbutton"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:text="Alert Dialog button"
        android:onClick="alertButton"/>
 </LinearLayout>

3. Main Activity code

package com.example.alertdialogandroid;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}
	 public void alertButton(View v)
	    {
		 AlertDialog.Builder alertDialog2 = new AlertDialog.Builder(
			        MainActivity.this);
			// Setting Dialog Title
			alertDialog2.setTitle("Confirm Delete...");
			// Setting Dialog Message
			alertDialog2.setMessage("Are you sure you want delete this file?");
			// Setting Icon to Dialog
			alertDialog2.setIcon(R.drawable.ic_launcher);
			// Setting Positive "Yes" Btn
			alertDialog2.setPositiveButton("YES",
			        new DialogInterface.OnClickListener() {
			            public void onClick(DialogInterface dialog, int which) {
			                // Write your code here to execute after dialog
			                Toast.makeText(getApplicationContext(),
			                        "You clicked on YES", Toast.LENGTH_SHORT)
			                        .show();
			            }
			        });
			// Setting Negative "NO" Btn
			alertDialog2.setNegativeButton("NO",
			        new DialogInterface.OnClickListener() {
			            public void onClick(DialogInterface dialog, int which) {
			                // Write your code here to execute after dialog
			                Toast.makeText(getApplicationContext(),
			                        "You clicked on NO", Toast.LENGTH_SHORT)
			                        .show();
			                dialog.cancel();
			            }
			        });
			// Showing Alert Dialog
			alertDialog2.show();
	    }
}

4. Demonstration
Main activity screen

Main activity screen
Main activity screen

Alert dialog screen

Alert dialog screen
Alert dialog screen

Alert dialog dismissed

Alert dialog dismissed
Alert dialog dismissed

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">