Friday, 19 October 2012

ANDROID - Date Picker Dialog

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/buttonDatePickerDialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="92dp"
        android:text="Show DatePickerDialog" />

</RelativeLayout>


2)Activity:

package com.venky.datepickerdialogdemo;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Toast;

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

      Button showDatePickerDialog;

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

            // set the references
            showDatePickerDialog = (Button) findViewById(R.id.buttonDatePickerDialog);

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

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

                        // get the Calendar
                        Calendar c = Calendar.getInstance();

                        // get the year,month,day
                        int cYear = c.get(Calendar.YEAR);
                        int cMonth = c.get(Calendar.MONTH);
                        int cDay = c.get(Calendar.DAY_OF_MONTH);

                        // create DatePickerDialog
                        DatePickerDialog datePickerDialog = new DatePickerDialog(
                                    DatePickerDialogDemoActivity.this, dateListener, cYear,
                                    cMonth, cDay);

                        // show DatePickerDialog
                        datePickerDialog.show();

                  }

                  // Listener for DatePickerDialog
                  OnDateSetListener dateListener = new OnDateSetListener() {

                        public void onDateSet(DatePicker view, int year,
                                    int monthOfYear, int dayOfMonth) {
                              // TODO Auto-generated method stub

                              int month = monthOfYear + 1;

                              Toast.makeText(
                                          DatePickerDialogDemoActivity.this,
                                          "Setted Date =>" + dayOfMonth + "-" + month + "-"
                                                      + year, Toast.LENGTH_LONG).show();
                        }
                  };
            });
      }
}


3)Download this Project Click Here

No comments:

Post a Comment