---
title: "DatePicker in Android"  
description: "1.	Date Picker is a control which provides user flexibility to pick the date.    2.	It has a month, day and year picking user interface format calenda"  
author: "Nitish Kesarwani"  
published: 2017-12-28  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/12748/datepicker-in-android  
category: "android apps"  
tags: ["android", "java", "date", "datetime"]  
reading_time: 4 minutes  

---

# DatePicker in Android

##### Description

1. [Date Picker](https://www.mindstick.com/blog/627/date-picker-using-bootstrap-in-asp-dot-net) is a control which provides user flexibility to pick the date.

2. It has a month, day and year picking [user interface](https://www.mindstick.com/blog/302016/what-is-user-interface-and-what-are-its-benefits) format [calendar](https://www.mindstick.com/articles/13088/how-to-choose-the-best-calendar-for-you-dynamics-crm-system).

3. It is a part of Widget

4. Button, datePicker and [TextView](https://www.mindstick.com/forum/23237/how-do-i-center-text-horizontally-and-vertically-in-a-textview-in-android) [controls](https://www.mindstick.com/articles/66/how-to-create-controls-at-run-time-in-csharp-dot-net) are used.

5. [onClickListener](https://www.mindstick.com/forum/33726/inside-onclicklistener-i-cannot-access-a-lot-of-things-how-to-approach) and datePicker() method is introduced to invoke the button class

XML Attributes\

• **[android](https://www.mindstick.com/articles/1567/navigation-drawer-implementation-in-android):calendarViewShow:** it checks the status of calendar i view.

• **android:datePickerMode**: it shows the look of widget.

• **android:****maxDate** : The maximum date shown by this calendar view.

• **android:minDate:** The minimum date shown by this calendar view.

• **android:spinnersShown :** It checks whether spinner is shown.

• **android:startYear :** it set first year of calender.

• **android:yearListItemTextAppearance** :To Change the list year's text appearance in the list.

• **android:yearListSelectorColor :** To change the list year's selected circle color in the list.

##### Methods

• **getDayOfMonth() :-** It returns the selected day of month.

• **getMonth() :**It get the selected month.

• **getYear()** : It returns the selected year.

• **getCalendarView():-**To get the calendar view..

###### Android_Manifest.xml

```
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mapsample.msclient009.date">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
```

###### Activity_main.xml

```
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.mapsample.msclient009.date.MainActivity"
    android:orientation="vertical">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <DatePicker
        android:id="@+id/dp"
        android:layout_width="200dp"
        android:layout_height="300dp"
        android:layout_weight="1" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    <TextView
        android:textColor="#199119"
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#F90"
        android:textSize="20dp"
        android:textColor="#f9f9f9"
        android:text="Date-Picker Tool" />
    </LinearLayout>
</LinearLayout>
```

\

###### Main_Activity.java

```
package com.mapsample.msclient009.date;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
 DatePicker dp;
 Button btn;
 TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate ( savedInstanceState );
        setContentView ( R.layout.activity_main );
        dp =(DatePicker)findViewById ( R.id.dp );
        btn =(Button)findViewById ( R.id.btn );
        tv =(TextView)findViewById ( R.id.tv );
          tv.setText (datePicker () );
        btn.setOnClickListener ( new View.OnClickListener ( ) {
            @Override
            public void onClick(View v) {
                tv.setText ( datePicker () );
            }
        } );
    }
    public String datePicker()
    {StringBuilder sb = new StringBuilder ( );
      sb.append ("Select Date");
      sb.append ( dp.getMonth ()+1+":" );
      sb.append (dp.getDayOfMonth ()+":");
      sb.append ( dp.getYear () );
      return (sb.toString ());
    }
}
```

![DatePicker in Android](https://www.mindstick.com/mindstickarticle/2ea17e00-77c4-484c-ae93-d0985301993e/images/eb2218f7-9ff8-4c56-a504-e7960481c797.png) ![DatePicker in Android](https://www.mindstick.com/mindstickarticle/2ea17e00-77c4-484c-ae93-d0985301993e/images/4e884c92-773f-4ba5-9310-c6c1fd657f91.png)\

---

Original Source: https://www.mindstick.com/articles/12748/datepicker-in-android

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
