Android DatePicker Example

By | April 8, 2013
Android SDK provides  android.widget.DatePicker class for creating datepicker dialog , In this  tutorial , we will see an example how to create a datepicker dialog on click of a Button and upon  selecting date from datepicker dialog will  display it in TextView.

When the date picker dialog popups it will disable the background activity until the user select the date .

Constructing Dialog

To construct Dialog we make use “DatePickerDialog” class , it creates a simple dialog containing an DatePicker.

DatePickerDialog(Context context, DatePickerDialog.OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth)

when the dialog gets prepared the ” onPrepareDialog(int id, Dialog dialog) ” is called and it updates the date which is set by user

Lets See An Example

Project Detail

Project Name DatepickerDemo
Package com.pavan.datepickerdemo
Minimum SDK API 8
Target SDK API 17
Theme Holo Light with Dark Action Bar

1. XML Layout

file :- date_picker_demo.xml

<RelativeLayout
        xmlns:android=”http://schemas.android.com/apk/res/android”
        xmlns:tools=”http://schemas.android.com/tools”
        android:layout_height=”match_parent”
        android:layout_width=”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=”.DatePickerDemo” >

        <TextView
                  android:id=”@+id/display_id”
                  android:layout_height=”wrap_content”
                  android:layout_width=”wrap_content”
                  android:text=”” />
   
        <Button
                android:id=”@+id/change_button_id”
                android:layout_below=”@+id/display”
                android:layout_height=”wrap_content”
                android:layout_margintop=”36dp”
                android:layout_torightof=”@+id/display”
                android:layout_width=”wrap_content”
                android:text=”@string/changedatebutton” />

</RelativeLayout>

2. Activity

file :- DatePickerDemo.java

package com.pavan.datepickerdemo;

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

public class DatePickerDemo extends Activity {

 Button change_date_but;
 TextView display_txt;
 public static final int Date_dialog_id = 1;
 // date and time
 private int mYear;
 private int mMonth;
 private int mDay;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
 
   setContentView(R.layout.date_picker_demo);
 
  change_date_but = (Button) findViewById(R.id.change_button_id);
  display_txt = (TextView) findViewById(R.id.display_id);
  change_date_but = (Button) findViewById(R.id.change_button_id);
  change_date_but.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
     DatePickerDialog DPD = new DatePickerDialog(
      DatePickerDemo.this, mDateSetListener, mYear, mMonth,
      mDay);
                    DPD.show();
   }
  });

  final Calendar c = Calendar.getInstance();
  mYear = c.get(Calendar.YEAR);
  mMonth = c.get(Calendar.MONTH);
  mDay = c.get(Calendar.DAY_OF_MONTH);

  updateDisplay();
 }

 @Override
 @Deprecated
 protected void onPrepareDialog(int id, Dialog dialog) {
  // TODO Auto-generated method stub
  super.onPrepareDialog(id, dialog);

  ((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);

 }

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

  public void onDateSet(DatePicker view, int year, int monthOfYear,
    int dayOfMonth) {
   mYear = year;
   mMonth = monthOfYear;
   mDay = dayOfMonth;
   updateDisplay();
  }
 };

 private void updateDisplay() {
  // TODO Auto-generated method stub
  display_txt.setText(new StringBuilder()
    // Month is 0 based so add 1
    .append(mMonth + 1).append(“-“).append(mDay).append(“-“)
    .append(mYear));
 }
}

3. RUN

Android DatePicker
  • @sam lekhi

    welcome 🙂

  • @nicolas

    add the following style tag inside style.xml
    <style name="AppBaseTheme" parent="android:Theme.Light">

    this style is taken by "<application>" tag of manifest.xml which applies to whole application

  • Nicolas

    Nice, it works thanks but i have the dark theme (Android awfull base theme) for the date picker dialog and i want to show the light and beautiful theme you used. Can you tell me how to have it ?

  • Works Perfectly..Thanks 🙂

  • Can you share what error your facing

  • Anonymous

    Its Not working .

  • Hi Pavan, I was facing one problem in Date Picker dialog, In my past project i am give multiple support of language through my application not phone language support. All kind of text will change when i am change the language but the datepicker month text is not change.
    I have to use the locale class for that and string.xml for different folder.
    if know please tell me.