Tallan's Technology Blog

Tallan's Top Technologists Share Their Thoughts on Today's Technology Challenges

Android Date Picker Dialog

While looking for the best way to implement an input widget to retrieve the users’s Date of Birth I came across this input field while editing a contacts information in the generic People List that comes with the Android OS:



When pressed this opens the DatePickerDialog as shown below:

The Android SDK provides a DatePickerDialog which (as its name suggests) can be used to choose dates in a dialog popup and seems to be what is used when editing contact information as shown above. While Googling I found implementations using text or edit views with buttons to show the DatePickerDialog but none that had the triangle in the bottom right corner (for ICS) indicating the user should press to edit the value. That was until I ran into this StackOverflow post.

I created the following Custom View:

package com.tallan.android.customviews;

import java.util.Calendar;

import android.app.DatePickerDialog;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;

public class DateDisplayPicker extends TextView implements DatePickerDialog.OnDateSetListener{

	private Context _context;

	public DateDisplayPicker(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		_context = context;
	}

	public DateDisplayPicker(Context context, AttributeSet attrs) {
		super(context, attrs);
		_context = context;
		setAttributes();
	}

	public DateDisplayPicker(Context context) {
		super(context);
		_context = context;
		setAttributes();
	}

	private void setAttributes() {
		setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				showDateDialog();
			}
		});
	}

	private void showDateDialog() {
		final Calendar c = Calendar.getInstance();
		DatePickerDialog dp = new DatePickerDialog(_context, this, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
		dp.show();
	}


	@Override
	public void onDateSet(DatePicker view, int year, int monthOfYear,
			int dayOfMonth) {
		setText(String.format("%s/%s/%s", monthOfYear, dayOfMonth, year));		
	}
}

Which can be used in a layout the same as a TextView as follows:

        <com.tallan.android.customviews.DateDisplayPicker
            android:id="@+id/clientEditCreate_BirthDateDayPicker"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            style="@android:style/Widget.Holo.Spinner"
            android:hint="Date"/>

The important part above is setting the style to “@android:style/Widget.Holo.Spinner” which makes it look like a spinner widget. Using it this way will provide the same look and feel as the contact edit info screen.

Thanks,
Nick

Tags: Android,

9 Comments. Leave new

Miguel Casallas
March 13, 2013 1:50 pm

Thankyou. very good post

pavan @(pavanh)

Hi
Very nice tutorial on datepicker and also you have very nice collection of tutorial

you can also this one

Date Picker

Time Picker

Thanks.

Because onDateSet takes monthOfYear as a 0-indexed integer (0-11) you actually need to add 1 to return the correct date string.

Correct code is:
setText(String.format(“%s-%s-%s”, year, monthOfYear+1, dayOfMonth));

http://developer.android.com/reference/android/app/DatePickerDialog.OnDateSetListener.html

Darshan Perera
March 23, 2014 1:28 am

Hi,

Thanks for the nice tutorial. Just curious to know whether this works and give the same appearance in older versions as well such as gingerbread.

Thank you.

Nice class!
Just an improvement to the code:

It doesn’t need the attribute “context” since the parent classe already stores the context. When you need inside the code, you can just call getContext().

Time picker in android studio
if you are beginner and do not know how to create time picker in android then this lecture can help

http://themasterworld.com/android-time-picker-example/

Nice XML! can you sent to me the full xml file of this example. Email: diegoaz3@gmail.com

I’m really inspired along wuth your writing abilities and also with the layout to your weblog.
Is this a paiid topic oor did you customize it yourself? Either way stay up the
nice high quality writing, it is uncommon to lokk a great wwblog ike this one these days..

Feel free to visit myy webpage :: Steps to Improving your credit

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>