Tuesday, 20 March 2012

Android Custom Alert Dialog

                                     Android Custom Alert Dialog

 1) AlertDialog is a small window that appears or pop up in front of your application.
 2) It gets the screen focus and able to accept user interaction.
 3) Dialogs are normally used for notifications that should interrupt the user and to perform short tasks.

Step 1:

Create a new project as CustomAlertDialog.
Copy  the below coding and paste it into your main.xml file.

  <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" >

        android:text="Alert Dialog" />


Step 2:

Copy the below code and paste into your java file(CustomAlertDialog.java).

package com.custom.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class CustomDialog extends Activity {
    Button buttonAlert;

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

        buttonAlert = (Button) this.findViewById(R.id.btn_alert);
        buttonAlert.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                AlertDialog.Builder alertBuilder = new AlertDialog.Builder(CustomDialog.this);
                alertBuilder.setCancelable(false).setPositiveButton("OK", new DialogInterface.OnClickListener()                            {
                            public void onClick(DialogInterface dialog, int id) {
                                // If press the OK button if finishes the dialog.

                AlertDialog alert = alertBuilder.create();
                alert.setTitle("Alert Title");
                alert.setMessage("Alert Message");

Full Source code for CustomAlertDialog

No comments:

Post a Comment