[TUT] Basic Date Picker

I was just looking on the Android Developer site and the only tutorial they had to show a DatePicker was within a dialog. I didn’t want my datepicker in a dialog, I wanted it embedded into my activity.

However I already knew there was a DatePicker XML view that I could use, so I was wondering why the hell doesn’t the tutorial explain it. I spun up a quick little app and had my datepicker on screen, now I just needed to get at those fields. I trawled the APIs looking for something like ‘onDateChanged’ but there was nothing I was so confused!

Then I realised the datepicker is so much simpler than that. You just add the XML to your layout file. And simply ask it what it’s values are! It’s so much easier than the examples on android developer. So here it is.

What we’re going to do:

  • Add the date picker to your layout
  • Get a global reference
  • Query the date pickers values on a button click

inline datepicker with toast of date

So here .. we .. go

Layout is a no brainer just add your DatePicker and a button that when pressed will ask the date picker what its values are.


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" >

        android:layout_height="wrap_content" />

        android:text="Select this date" />

Then we have our Activity, this will display a toast when the button is pressed with the current day/month/year. Sorry my american friends I’ve stuck with my roots and my english date format 🙂 p.s. don’t forget the onClick of the button is set in the XML layout.


package com.blundell.tut.ui.phone;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.Toast;

import com.blundell.tut.R;

public class MainActivity extends Activity  {
	private DatePicker picker;

	/** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        picker = (DatePicker) findViewById(R.id.datePicker);
    public void onDateSelectedButtonClick(View v){
    	int day = picker.getDayOfMonth();
    	int month = picker.getMonth();
    	int year = picker.getYear();
    	Toast.makeText(this, day+"/"+month+"/"+year, Toast.LENGTH_SHORT).show();

Hope you enjoy, haven’t added any source code this time as the code is pretty short, but if you think this is wrong and want the DatePicker Tutorial eclipse project source, just comment and I’ll hang it up.

22 thoughts on “[TUT] Basic Date Picker

  1. i want to set notification on particular date and time inbuilt in program , do not want set it from user side. can anyone suggest me how to set it in java file ?

  2. very simple, the best datepicker ever posted….i spent hours trying to figure out the other tutorials just to get the values of mm/dd/yy….very very nice…keep on posting…

      1. it just doesnt allow it for some reason.
        it says this:
        Multiple annotations found at this line:
        – The markup in the document following the root element must be well-

    1. If you think that really is the case change this int month = picker.getMonth(); to this int month = picker.getMonth() + 1;

  3. I was looking for an example like this. I found it after going through about 10 other. Straight, simple, to the point. Thank you 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *