Learn About Date Picker in Android

Introduction

This article explains the Date Picker in Android. Android Studio is used to create the sample.

The date picker picks a date from your device. To pick the date you need to use a DatePicker in your XML file. This Date Picker will pick the date using the Calender class. First you will create the object of the Calender class using the getInstance() method. Now you will get the year by calling the static variable YEAR of the Calender class that returns the current year and doing the same to get the date nad month.

 final Calendar calender = Calendar.getInstance();
        year = calender.get(Calendar.YEAR);
        month = calender.get(Calendar.MONTH);
        day = calender.get(Calendar.DAY_OF_MONTH);

Now initialize the year, month and day into the datePicker by calling init().

   datePickerpResult.init(year, month, day, null);

Step 1
 

Create a project like this:

Clipboard01.jpg
 

 

Step 2

Create an XML file and wirte this:
 

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

              android:layout_width="fill_parent"

              android:layout_height="fill_parent"

              android:orientation="vertical" >

 

    <Button

            android:id="@+id/ChangeDate"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text=" Date Change" />

 

    <TextView

            android:id="@+id/textView1Date"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="Current Date"

            android:textAppearance="?android:attr/textAppearanceLarge" />

 

    <TextView

            android:id="@+id/textView2Date"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text=""

            android:textAppearance="?android:attr/textAppearanceLarge" />

 

    <DatePicker

            android:id="@+id/datePicker"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content" />

 

</LinearLayout>

Step 3

Create a Java file and wirte this:

 

package com.datepickerexample;

 

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

import java.util.Calendar;

import android.app.Activity;

import android.app.DatePickerDialog;

import android.app.Dialog;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.DatePicker;

import android.widget.TextView;

 

public class MainActivity extends Activity {

 

    private TextView tvDisplayDate;

    private DatePicker datePickerpResult;

    private Button btnChangeDate;

 

    private int year;

    private int month;

    private int day;

 

    static final int DATE_ID = 999;

 

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        setDateOnActivity();

        addListenerOnButton();

    }

    // display current date

    public void setDateOnActivity() {

 

        tvDisplayDate = (TextView) findViewById(R.id.textView1Date);

        datePickerpResult = (DatePicker) findViewById(R.id.datePicker);

        final Calendar calender = Calendar.getInstance();

        year = calender.get(Calendar.YEAR);

        month = calender.get(Calendar.MONTH);

        day = calender.get(Calendar.DAY_OF_MONTH);

        // set current date into textview

        tvDisplayDate.setText(new StringBuilder()

                // Month is 0 based, just add 1

                .append(month + 1).append("-").append(day).append("-")

                .append(year).append(" "));

        // set current date into datepicker

        datePickerpResult.init(year, month, day, null);

    }

    public void addListenerOnButton() {

        btnChangeDate = (Button) findViewById(R.id.btnChangeDate);

        btnChangeDate.setOnClickListener(new OnClickListener() {

            @Override

            public void onClick(View v) {

                showDialog(DATE_ID);

            }

        });

    }

 @Override

    protected Dialog onCreateDialog(int i) {

        switch (i) {

            case DATE_ID:

                // set date picker as current date

                return new DatePickerDialog(this, datePickerListener,

                        year, month,day);

        }

        return null;

    }

 

    private DatePickerDialog.OnDateSetListener datePickerListener

            = new DatePickerDialog.OnDateSetListener() {

        // when dialog box is closed, below method will be called.

        public void onDateSet(DatePicker view, int Year,

                              int Month, int Day) {

            year = Year;

            month = Month;

            day = Day;

            // set selected date into textview

            tvDisplayDate.setText(new StringBuilder().append(month + 1)

                    .append("-").append(day).append("-").append(year)

                    .append(" "));

            // set selected date into datepicker also

            datePickerpResult.init(year, month, day, null);

        }

    };

}
 

Step 4


Android Manifest.xml file:
 

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.datepickerexample"

    android:versionCode="1"

    android:versionName="1.0" >

 

    <uses-sdk

        android:minSdkVersion="7"

        android:targetSdkVersion="16" />

 

    <application

        android:allowBackup="true"

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <activity

            android:name="com.datepickerexample.MainActivity"

            android:label="@string/app_name" >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

 

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>

 

</manifest>

Step 5

Display the currrent date of the device:

Clipboard02.jpg


When you click on the date change button to change the date:

Clipboard03.jpg


After selecting the day, month and year the Date has changed as in the following:


Clipboard04.jpg


Recommended Free Ebook
Similar Articles