Cdacians

Cdacians
Cdacians

Sunday, 26 February 2012

Alert Dialog Example in Android


Description:
This example shows how you can an alert dialog box.
Algorithm:
1.) Create a new project by File-> New -> Android Project name it AlertDialogExample.
2.) Run for output.
Steps:
1.) Create a project named AlertDialogExample and set the information as stated in the image.
Build Target: Android 2.3.3
Application Name: AlertDialogExample
Package Name: com.example
Activity Name: AlertDialogExample
Min SDK Version: 10
2.) Open AlertDialogExample.java file and write following code there:
package com.example;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
public class AlertDialogExample extends Activity {
    /** Called when the activity is first created. */
        @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
        alt_bld.setMessage("Do you want to close this application ?")
        .setCancelable(false)
        .setPositiveButton("Yes"new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
        finish();
        }
        })
        .setNegativeButton("No"new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
        //  Action for ‘NO’ Button
        dialog.cancel();
}
        });
        AlertDialog alert = alt_bld.create();
        // Title for AlertDialog
        alert.setTitle("AlertDialogExample");
        // Icon for AlertDialog
        alert.setIcon(R.drawable.ic_launcher);
        alert.show();
        }
        }
3.) Compile and build the project.
Output


Thanks
akm
www.cdacians.com

No comments:

Post a Comment