Monday 22 October 2012

ANDROID - Dialogs Demo


1)main.xml:


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

    <Button
        android:id="@+id/btnAlertDialog"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:gravity="center"
        android:text="Alert Dialog" />

    <Button
        android:id="@+id/btnProgressDialog"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Progress Dialog" />

    <Button
        android:id="@+id/btnDatePickerDialog"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Date Picker Dialog" />

    <Button
        android:id="@+id/btnTimePickerDialog"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Time Picker Dialog" />

</LinearLayout>

2)Activity:


package com.venkool;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;

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

      Button btnAlertDialog, btnProgressDialog, btnDatePickerDialog,
                  btnTimePickerDialog;

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

            btnAlertDialog = (Button) findViewById(R.id.btnAlertDialog);

            btnProgressDialog = (Button) findViewById(R.id.btnProgressDialog);

            btnDatePickerDialog = (Button) findViewById(R.id.btnDatePickerDialog);

            btnTimePickerDialog = (Button) findViewById(R.id.btnTimePickerDialog);

            btnAlertDialog.setOnClickListener(new OnClickListener() {

                  public void onClick(View v) {

                        AlertDialog.Builder alert = new AlertDialog.Builder(
                                    DialogsDemoActivity.this);

                        alert.setMessage("Do you Want to Continuee ...")
                                    .setPositiveButton("Yes",
                                                new DialogInterface.OnClickListener() {

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

                                                            Toast.makeText(getApplicationContext(),
                                                                        "You Clicked => Yes", 5000)
                                                                        .show();
                                                      }
                                                })
                                    .setNegativeButton("No",
                                                new DialogInterface.OnClickListener() {

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

                                                            Toast.makeText(getApplicationContext(),
                                                                        "You Clicked => No", 5000)
                                                                        .show();
                                                      }
                                                }).show();

                  }

            });

            btnProgressDialog.setOnClickListener(new OnClickListener() {

                  @Override
                  public void onClick(View v) {

                        ProgressDialog progress = new ProgressDialog(
                                    DialogsDemoActivity.this);
                        progress.setMessage("Loading...");
                        progress.show();
                  }
            });

            btnDatePickerDialog.setOnClickListener(new OnClickListener() {

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

                        Calendar c = Calendar.getInstance();
                        int cYear = c.get(Calendar.YEAR);
                        int cMonth = c.get(Calendar.MONTH);
                        int cDay = c.get(Calendar.DAY_OF_MONTH);

                        DatePickerDialog date = new DatePickerDialog(
                                    DialogsDemoActivity.this, dateListener, cYear, cMonth,
                                    cDay);
                        date.show();
                  }
            });

            btnTimePickerDialog.setOnClickListener(new OnClickListener() {

                  @Override
                  public void onClick(View v) {

                        Calendar c = Calendar.getInstance();
                        int cHour = c.get(Calendar.HOUR_OF_DAY);
                        int cMinute = c.get(Calendar.MINUTE);

                        TimePickerDialog time = new TimePickerDialog(
                                    DialogsDemoActivity.this, timeListener, cHour, cMinute,
                                    false);
                        time.show();
                  }
            });
      }

      OnDateSetListener dateListener = new OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear,
                        int dayOfMonth) {

                  int month = monthOfYear + 1;

                  Toast.makeText(
                              DialogsDemoActivity.this,
                              "Selected Date is" + dayOfMonth + "-" + month + "-"
                                          + year, 5000).show();
            }
      };

      OnTimeSetListener timeListener = new OnTimeSetListener() {

            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

                  Toast.makeText(DialogsDemoActivity.this,
                              "Selected Time is :" + hourOfDay + ":" + minute, 5000)
                              .show();
            }
      };
}


3)Download this Project Click Here


Demo :


 



No comments:

Post a Comment