Monday, 2 September 2013

Alert Dialog Android

Aler Dialog Android 






Hi friends today we will se how to create simple alert dialog in Android.
Its very simple , we can create alert dailog with 1 , 2 or 3 buttons
here is sample code.


package com.example.dailogboxexample;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
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 MainActivity extends Activity {
       Context context;

       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);

              context = this;

              Button button = (Button) findViewById(R.id.button1);
              button.setOnClickListener(new OnClickListener() {
                     @Override
                     public void onClick(View v) {

                           new AlertDialog.Builder(context)
                                         .setTitle("Alert Box ")
                                         .setMessage(
                                                       "Please check your Internet conection, and try again?")
                                         .setPositiveButton("Ok",
                                                       new DialogInterface.OnClickListener() {
                                                              public void onClick(DialogInterface dialog,
                                                                           int which) {

                                                                     Toast.makeText(context, "Ok Pressed", 1)
                                                                                  .show();
                                                              }
                                                       }).show();
                     }
              });

              Button button2 = (Button) findViewById(R.id.button2);
              button2.setOnClickListener(new OnClickListener() {
                     @Override
                     public void onClick(View v) {

                           new AlertDialog.Builder(context)
                                         .setTitle("Alert Box ")
                                         .setMessage(
                                                       "Please check your Internet conection, and try again?")
                                         .setPositiveButton("Yes",
                                                       new DialogInterface.OnClickListener() {
                                                              public void onClick(DialogInterface dialog,
                                                                           int which) {
                                                                     Toast.makeText(context, "Yes Pressed",
                                                                                  1).show();
                                                              }
                                                       })
                                         .setNegativeButton("No",
                                                       new DialogInterface.OnClickListener() {
                                                              public void onClick(DialogInterface dialog,
                                                                           int which) {
                                                                     // do nothing
                                                                     Toast.makeText(context, "No Pressed", 1)
                                                                                  .show();
                                                              }
                                                       }).show();
                     }
              });

              Button button3 = (Button) findViewById(R.id.button3);
              button3.setOnClickListener(new OnClickListener() {
                     @Override
                     public void onClick(View v) {

                           new AlertDialog.Builder(context)
                                         .setTitle("Alert Box ")
                                         .setMessage(
                                                       "Please check your Internet conection, and try again?")
                                         .setPositiveButton("Yes",
                                                       new DialogInterface.OnClickListener() {
                                                              public void onClick(DialogInterface dialog,
                                                                           int which) {
                                                                     Toast.makeText(context, "Yes Pressed",
                                                                                  1).show();
                                                              }
                                                       })
                                         .setNegativeButton("No",
                                                       new DialogInterface.OnClickListener() {
                                                              public void onClick(DialogInterface dialog,
                                                                           int which) {
                                                                     // do nothing
                                                                     Toast.makeText(context, "No Pressed", 1)
                                                                                  .show();
                                                              }
                                                       })
                                         .setNeutralButton("Cancel",
                                                       new DialogInterface.OnClickListener() {
                                                              public void onClick(DialogInterface dialog,
                                                                           int which) {
                                                                     // do nothing
                                                                     Toast.makeText(context,
                                                                                  "Cancel Pressed", 1).show();
                                                              }
                                                       }).show();

                     }
              });

       }
}


and here is xml file for buttons layout.


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button2"
        android:layout_centerVertical="true"
        android:text="3 Button Alert" />

    <Button
        android:id="@+id/button2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button3"
        android:layout_alignLeft="@+id/button1"
        android:text="2 Button Alert " />

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button2"
        android:layout_alignParentLeft="true"
        android:text="1 Button Alert" />

</RelativeLayout>

simple :)
Happy Codding :) 


No comments:

Post a Comment