[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.

activity_main.xml

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

    <DatePicker
        android:id="@+id/datePicker"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/selectButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="onDateSelectedButtonClick"
        android:text="Select this date" />
    
</LinearLayout>

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.

MainActivity.class

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. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        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. Hm…. Didn’t know that a DatePicker control has an associated XML, and it’s so easy to retrieve its data.
    Thanks for posting this.

Comments are closed.