Thursday 18 October 2012

ANDROID - Alert Dialog Example

Demo:


1)main.xml:



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#9932CC"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="80dp"
        android:textSize="20dp"
        android:text="Show AlertDialog" />

</RelativeLayout>

2)Activity:



package com.venky.alertDialogdemo;

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;
import android.widget.Toast;

public class AlertDialogDemoActivity extends Activity {
      /** Called when the activity is first created. */

      Button showAlertDialog;

      @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            // call the references
            showAlertDialog = (Button) findViewById(R.id.button1);

            // set the Listener
            showAlertDialog.setOnClickListener(new OnClickListener() {

                  public void onClick(View v) {
                        // TODO Auto-generated method stub

                        // Creating AlertDialog
                        AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                                    AlertDialogDemoActivity.this);

                        // set Icon
                        alertDialog.setIcon(R.drawable.ic_launcher);

                        // set Title
                        alertDialog.setTitle("Alert Dialog");

                        // set Message
                        alertDialog.setMessage("This is AlertDialog");

                        // set Positive Button
                        alertDialog.setPositiveButton("OK",
                                    new DialogInterface.OnClickListener() {

                                          public void onClick(DialogInterface dialog,
                                                      int which) {
                                                // TODO Auto-generated method stub

                                                Toast.makeText(getApplicationContext(), "OK",
                                                            Toast.LENGTH_LONG).show();
                                          }
                                    });

                        // set Negetive Button
                        alertDialog.setNegativeButton("No",
                                    new DialogInterface.OnClickListener() {

                                          public void onClick(DialogInterface dialog,
                                                      int which) {
                                                // TODO Auto-generated method stub

                                                Toast.makeText(getApplicationContext(), "No",
                                                            Toast.LENGTH_LONG).show();
                                          }
                                    });

                        // set Neutral Button
                        alertDialog.setNeutralButton("Cancel",
                                    new DialogInterface.OnClickListener() {

                                          public void onClick(DialogInterface dialog,
                                                      int which) {
                                                // TODO Auto-generated method stub

                                                Toast.makeText(getApplicationContext(),
                                                            "Cancel", Toast.LENGTH_LONG).show();
                                          }
                                    });

                        // show AlertDialog
                        alertDialog.show();
                  }
            });
      }
}



3)Download this Project Click Here

2 comments: