Using Android Date Picker

This is a sample activity which shows how to use Date picker control.

Underlying Algorithm:

Basic description of algorithm in step by step form:
1.) Create a Project DatePickerExample.
2.) Create a date_picker.xml in res/layout.
3.) Put the following code in date_picker.xml :

 

 

 

4.) To create a date picker control we need the following imports :

 

 

 

5.) Run the application.

Steps to Create:

1.) Open Eclipse. Use the New Project Wizard and select Android Project Give the respective project name i.e. DatePickerExample. Enter following information:
Project name: DatePickerExample
Build Target: Google APIs
Application name: DatePickerExample
Package name: com.sample.DatePickerExample
Create Activity: DatePickerExample

On Clicking Finish DatePickerExample code structure is generated with the necessary Android Packages being imported along with DatePickerExample.java. DatePickerExample class will look like following:

 

 

 

Output –The final output:

Leave a Comment:

11 comments
Arif says June 10, 2011

Thanks dude,that worked well…….

just wanted to know the other thing how will i embed this date function with other application of android

Reply
    Sushant says June 10, 2011

    You can call below lines anywhere in other android application and use mYear/mMonth/mDay to play with date.

    [code lang="java"]
    final Calendar c = Calendar.getInstance();
    mYear = c.get(Calendar.YEAR);
    mMonth = c.get(Calendar.MONTH);
    mDay = c.get(Calendar.DAY_OF_MONTH);
    [/code]

    I hope I am getting you correctly.

    Reply
Tester says October 28, 2011

Is that any style has to be applied in order to get the above date picker rather than standard datepicker dialog?

Reply
Tester says October 28, 2011

Is that any style has to be applied in order to get the above date picker rather than standard date picker dialog?..

Reply
Derek Davidson says December 23, 2011

I get the same as you, but it is difficult to change the year. I have been searching how toget a ‘nicer’ dialog, and it may be to set the them parameter. But I am not sure what tis points to.. Is it an XML file that defines the dialog.

I know the problem with the dialog is that it is too big for the screen. Rotating sometimes shows a ‘scrollable’ year, or part of the text for the year. (a lot of 20 to gues which one is 2004).

Any clues on where to look for a them for a dateDialog?

Reply
Derek Davidson says December 23, 2011

I get the same as you, but it is difficult to change the year. I have been searching how toget a ‘nicer’ dialog, and it may be to set the them parameter. But I am not sure what tis points to.. Is it an XML file that defines the dialog.

I know the problem with the dialog is that it is too big for the screen. Rotating sometimes shows a ‘scrollable’ year, or part of the text for the year. (a lot of 20 to gues which one is 2004).

Any clues on where to look for a them for a dateDialog?

Reply
Pavan Kumar says April 8, 2013

very nicely explained thank you sir

Reply
Sebastian C says May 26, 2013

How to modify it to pick from array of colours instead of text?

Reply
Meghna says July 21, 2013

Perfect thanks… This http://www.compiletimeerror.com/2013/07/android-date-picker-example-android.html looks a bit different, may help.. Have a look…

Reply
Ankur says July 30, 2013

Hi, How I Can add Two Button Simultaneously In Single Layout Activity.
And Both Calander Sholud Display Different Date in Both Text View.
Give Me Code If Requre Any New Plz.

Thanx In Advance.

Reply
android : error in datepicker | Technology & Programming says November 10, 2013

[…] am using this link to provide a date picker in my android […]

Reply
Add Your Reply