Use DatePicker on Android

In this post I’m going to discuss how to use date picker on your android app. So what is Date Picker and when do you want to use?

Date Picker: It provides controls to the users to pick a date. This date picker provides controls for selecting each part of the date (month, day, and year). We can get valid and formatted date by using this control.

Normally we are using date picker, wherever we need to get the date.

I am going to use DialogFragment for creating the dialog date picker. In my example I’m going to show a button and when you click on that, it will show the date selection dialog. Once you select the date, it will show selected date.

First design UI the using XML.

<?xml version=“1.0” encoding=“utf-8”?>
<RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“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=“com.learnlinky.datepicker.MainActivity”>

<Button
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“@string/btn_set_date”
android:id=“@+id/btnSetDate”
android:layout_centerVertical=“true”
android:layout_centerHorizontal=“true”
android:textStyle=“bold” />

<TextView
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:textAppearance=“?android:attr/textAppearanceLarge”
android:text=“@string/lbl_date_msg”
android:id=“@+id/txtDate”
android:layout_above=“@+id/btnSetDate”
android:layout_centerHorizontal=“true”
android:layout_marginBottom=“20dp”
android:gravity=“center”
android:textSize=“28sp” />
</RelativeLayout>

 

datepicker_xml_learnlinky

 

Extending the DialogFragment for DatePicker

Override the onCreateDialog() method to return an instance of DatePickerDialog and implements the  DatePickerDialog.OnDateSetListener interface to get the user selected date.

 

android_calender

Now we are going to see how to change the color of the DatePicker? So I am going to write a style

Pass your custom style to DatePickerDialog object.

android_DatePicker_color_change

Now will see, how to change the theme of the datepicker

Available themes

  • THEME_DEVICE_DEFAULT_DARK
  • THEME_DEVICE_DEFAULT_LIGHT
  • THEME_HOLO_DARK
  • THEME_HOLO_LIGHT
  • THEME_TRADITIONAL

new DatePickerDialog(getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_DARK,this, year, month, day);
THEME_DEVICE_DEFAULT_DARK

 

new DatePickerDialog(getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT,this, year, month, day);
THEME_DEVICE_DEFAULT_LIGHT

 

new DatePickerDialog(getActivity(), AlertDialog.THEME_HOLO_DARK,this, year, month, day);

THEME_HOLO_DARK
new DatePickerDialog(getActivity(), AlertDialog.THEME_HOLO_LIGHT,this, year, month, day);

THEME_HOLO_LIGHT
new DatePickerDialog(getActivity(), AlertDialog.THEME_TRADITIONAL,this, year, month, day);

THEME_TRADITIONAL

 

Set DialogDatePicker Title

 

DatePickerDialog Title

Next time I going to discuss how to set date rage selection, I mean set minimum maximum sate and hide year or month on date picker.

If you feel this article is useful then don’t forget to put your comments or suggestions.

 

3 Comments

  1. Bavan August 11, 2016
  2. Andy June 16, 2017
  3. Tester September 10, 2017

Add Comment