As in our last tutorial we discussed about Time picker Dialog, Now in this tutorial I am going to discuss about How to implement date picker in android app ?

Android provides controls for the user to pick a Time or pick a date as ready-to-use dialogs. Each picker provides controls for selecting each part of the date (month, day, year). Using these pickers helps ensure that your users can pick a date that is valid, formatted correctly, and adjusted to the user’s locale.

DatePicker is a kind of widget which is used pick a particular date from the calender. DatePicker is a dialog that prompts the user for the date of day using a DatePicker.

1. Create a New Project :

Lets we start with a new project in Android Studio from File New Project ⇒
select Empty Activity and go through it.

2. Create a button to your main layout file :

Here we create a button to your main layout file i.e. activity_main.xml of your Main_Activity.java file or just implement the following xml code.

<Button
        android:id="@+id/mDatePicker"
        android:text="Date Picker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="20dp"
        android:layout_centerInParent="true"
        android:textStyle="bold"
        android:textSize="22sp"
        android:background="@android:color/holo_green_light"/>

activity_main.xml  :

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

    <Button
        android:id="@+id/mDatePicker"
        android:text="Date Picker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="20dp"
        android:layout_centerInParent="true"
        android:textStyle="bold"
        android:textSize="22sp"
        android:background="@android:color/holo_green_light"/>

</RelativeLayout>

3. Now open your MainActivity.java and add following code :

Now we create a Date picker Dialog view in android when we click on button using setOnClickListener( )  .

MainActivity.java :

package com.technxtcodelabs;

import android.app.DatePickerDialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;


import java.util.Calendar;


public class MainActivity extends AppCompatActivity {

    Button mDatePicker;
    DatePickerDialog.OnDateSetListener mOnDateSetListener;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

        mDatePicker.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar mCalendar =  Calendar.getInstance();
                int year = mCalendar.get(Calendar.YEAR);
                int month = mCalendar.get(Calendar.MONTH);
                int day = mCalendar.get(Calendar.DAY_OF_MONTH);

                DatePickerDialog mDatePickerDialog = new DatePickerDialog(
                        MainActivity.this,
                        android.R.style.Theme_Holo_Light_Dialog_MinWidth,
                        mOnDateSetListener,
                        year,month,day);
                mDatePickerDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                mDatePickerDialog.show();

            }
        });

        mOnDateSetListener = new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                month = month+1;
                String mDate = dayOfMonth+"/"+month+"/"+year;
                mDatePicker.setText(mDate);
            }
        };

    }
}

Now just run the project/app to test date picker dialog in android app through android device or any Emulator . 

%d bloggers like this: