Android DatePicker

| Page Views: 25

Android DatePicker Tutorials and Examples.

A DatePicker is a widget that allows us select a date.

It's existed API Level 1 and derives from FrameLayout.

You can use the DatePicker to select year, month and day. You can use a Spinner or CalenderView as long as you set the R.styleable.DatePicker_datePickerMode attribute.

The set of spinners and the calendar view are automatically synchronized. The client can customize whether only the spinners, or only the calendar view, or both to be displayed.

When the R.styleable.DatePicker_datePickerMode attribute is set to calendar, the month and day can be selected using a calendar-style view while the year can be selected separately using a list.

DatePickerDialog is a dialog that makes use of DatePicker.

DatePicker API Definition

DatePicker has been around since the beginning of android, API Level 1.

It's a class deriving from the FrameLayout.

public class DatePicker extends FrameLayout 

Let's see datepicker's inheritance hierarchy:

   ↳    android.view.View
       ↳    android.view.ViewGroup
           ↳    android.widget.FrameLayout
               ↳    android.widget.DatePicker

Quick DatePicker Examples

Android DatePicker Examples

Example 1

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.DatePicker;
import android.widget.TimePicker;

import java . useful . Calendar ;

public class MainActivity extends AppCompatActivity {      

    private TimePicker timePicker ;   
    private DatePicker datePicker;
    Private Calendar calendar ;  
    private int year;
    private int month;
    private int day ;  
    private int hour;
    private int minute;
    protected void onCreate(Bundle savedInstanceState) {
        / / Get an object of the calendar 
        calendar = Calendar.getInstance();
        / / Get the year, month, day, minute, second 
        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH);
        day = calendar.get(Calendar.DAY_OF_MONTH);
        hour = calendar.get(Calendar.HOUR);
        minute = calendar.get(Calendar.MINUTE);
        setTitle(year+"-"+ month%12 +"-"+day+"-"+" "+hour+":"+minute);
        datePicker = (DatePicker) findViewById(;
        timePicker = (TimePicker) findViewById(;

                year, month, day, new DatePicker.OnDateChangedListener() {
                    public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
                setTitle(hourOfDay + ":" + minute);

        new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                setTitle(year + "-"+(monthOfYear+1)+"-"+dayOfMonth);

        new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    android:layout_width="match_parent" android:layout_height="match_parent"

        android:id="@+id/datePicker" />

        android:layout_gravity="bottom" />

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:


What do You Think

Previous Post Next Post