Android DatePicker

DatePicker can be defined as below –

"In Android, DatePicker is a widget used to choose a date. It allows to choose your custom UI (user interface) date by day, month and year in your custom UI. If we need to show this view as a dialog then we've to use a DatePickerDialog class."

Some of the popular attributes of android DatePicker are –

 

 

S. No. XML Attributes Description
1 android:id This is unique id of the DatePicker to uniquely identify the DatePicker.
2 android:height Height of the DatePicker.
3 android:width Width of the DatePicker.
4 android:calendarTextColor Defines text color of the calendar.
5 android:calendarViewShown Checks whether calendar view is shown or not.
6 android:datePickerMode Defines the look of the DatePicker.
7 android:dayOfWeekBackground Defines the background color for the header’s day of week.
8 android:endYear Defines the last year (inclusive) of the calendar.
9 android:firstDayOfWeek Defines the first day of the week according to calendar.
10 android:headerBackground Defines background for the selected date header.
11 android:maxDate Defines the maximal date shown by the calendar in mm/dd/yyyy format.
12 android:minDate Defines the minimal date shown by the calendar in mm/dd/yyyy format.
13 android:spinnersShown Specifies whether spinners are shown or not.
14 android:startYear Defines the first year (inclusive) of the calendar.
15 android:fadeScrollbars Defines whether scrollbar should fade out when not in use or not.
16 android:fitsSystemWindows Defines whether to adjust view layout according to system windows.
17 android:minHeight Defines minimum height.
18 android:minWidth Defines minimum width.
19 android:padding Defines padding of the view.
20 android:visibility Defines visibility of the view.

At first, we will create android application. Then, we will use DatePicker in the application.

Follow steps below to create new project. Please ignore the steps if you've already created a new application.

 

 

S. No. Steps
1 Open Android Studio.
2 Click on Start a new Android Studio Project Write application name as DatePicker. Then, click next button.
3 Select minimum SDK you need. However, we have selected 14 as minimum SDK. Then, click next button.
4 Then, select Empty Activity => click next => click finish.
5 If you have followed above process correctly, you will get a newly created project successfully. However, you can also visit tutorial  to Create a New Project to know steps in detail.

Now, we will modify xml and java file to use DatePicker in the application.

No values folder have been modified. So, we're not going to mention them here.

Open res/layout/activity_main.xml file. Then, add below code into it.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_margin="10dp"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp" />

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

</LinearLayout>

In activity_main.xml file, we have defined TextView and DatePicker in android application. Now, we will access these TextView and DatePicker in java file.

 

 

Open src/main/java/com.ukacademe.datepicker/MainActivity.java file. Then, add below code into it.

package com.ukacademe.datepicker;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final TextView textView = findViewById(R.id.textView);

        DatePicker datePicker = findViewById(R.id.datePicker);
        if (datePicker != null) {
            Calendar today = Calendar.getInstance();
            datePicker.init(today.get(Calendar.YEAR), today.get(Calendar.MONTH), today.get(Calendar.DAY_OF_MONTH),

                    new DatePicker.OnDateChangedListener() {

                        @Override
                        public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                            String msg = "Selected Date is " + dayOfMonth + "/" + monthOfYear + "/" + year;
                            Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();

                            if (textView != null) {
                                textView.setText(msg);
                            }
                        }
                    });
        }
    }
}

In MainActivity.java file, we have accessed TextView and DatePicker. Then, we've set date change listener to display selected date in TextView.

Since the file of AndroidManifest.xml is very important in any android application, we are also going to see the content inside this file.

Code inside src/main/AndroidManifest.xml file is as below –

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

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

When we run the program, we will get output as shown below.