Monday 21 January 2013

Date Picker Example in Android


activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <Button
        android:id="@+id/date_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="43dp"
        android:layout_marginTop="34dp"
        android:text="Set Date" />

</RelativeLayout>

MainActivity.java


package com.rajeshvijayakumar.datepicker;

import java.util.Calendar;

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

public class MainActivity extends Activity implements OnClickListener {

    private Button mDateButton;

    private Calendar mCalen;
    private int day;
    private int month;
    private int year;

    private static final int DATE_PICKER_ID = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDateButton = (Button) findViewById(R.id.date_button);
        mCalen = Calendar.getInstance();
        day = mCalen.get(Calendar.DAY_OF_MONTH);
        month = mCalen.get(Calendar.MONTH);
        year = mCalen.get(Calendar.YEAR);
        mDateButton.setOnClickListener(this);
    }

    @Override
    @Deprecated
    protected Dialog onCreateDialog(int id) {

        switch (id) {
            case DATE_PICKER_ID:
                return new DatePickerDialog(this, datePickerListener,
                        year, month, day);
        }
        return null;
    }

    private DatePickerDialog.OnDateSetListener datePickerListener =
            new DatePickerDialog.OnDateSetListener() {

                // while dialog box is closed, below method is called.
                public void onDateSet(DatePicker view, int selectedYear,
                        int selectedMonth, int selectedDay) {
                    year = selectedYear;
                    month = selectedMonth;
                    day = selectedDay;

                    // Set the Date String in Button
                    mDateButton.setText(day + " / " + (month + 1) + " / " + year);
                }
            };

    @Override
    public void onClick(View v) {
        showDialog(DATE_PICKER_ID);

    }
}


Output :







Source Code : Download this Example Here




5 comments:

  1. sir,m gettin deprecated warning on showDialog how to resolve it?
    please help urgent

    ReplyDelete
  2. sir, i am gettin deprecated error on showDialog..please help urgent

    ReplyDelete
  3. is it valid to use depricated methods for latest versions??????

    ReplyDelete
    Replies
    1. This is old one, don't use this, better you can try ics pickers available in github

      Delete