Android Date Picker Example

In this tutorial you will get android date picker example.

DatePickerDialog is used to show a dialog to pick a date. To show a DatePickerDialog call showDialog() with unique DatePickerDialog id.

onCreateDialog() method is automatically called on calling showDialog() method.

Register OnDateSetListener with DatePickerDialog and override onDateSet() method.

onDateSet() contains the picked date. I am setting the picked date in textview using displayDate() method.

Calendar class is used to get current date.

 

Android Date Picker Example

Create a new android studio project with package name com.datepickerexample.

Create a layout file inside res/layout folder and add following code inside it.

activity_main.xml

<?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:paddingBottom="15dp"
    android:paddingLeft="15dp"
    android:paddingRight="15dp"
    android:paddingTop="15dp"
    tools:context="com.datepickerexample.MainActivity"
    android:orientation="vertical"
    android:gravity="center">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Set Date"
        android:id="@+id/setDateBtn"
        android:layout_marginBottom="10dp"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/selectedDateTxt"/>
</LinearLayout>

 

Create a java source file inside package and add following code.

MainActivity.java

package com.datepickerexample;

import android.app.DatePickerDialog;
import android.app.Dialog;
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;
import java.util.Calendar;

public class MainActivity extends AppCompatActivity {
    Button setDateBtn;
    TextView selectedDateTxt;
    int day, month, year;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setDateBtn = (Button)findViewById(R.id.setDateBtn);
        selectedDateTxt = (TextView)findViewById(R.id.selectedDateTxt);

        //get current date
        Calendar c = Calendar.getInstance();
        day = c.get(Calendar.DAY_OF_MONTH);
        month = c.get(Calendar.MONTH);
        year = c.get(Calendar.YEAR);

        //set date in textview
        displayDate(day, month, year);

        setDateBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog(111);
            }
        });
    }

    void displayDate(int d, int m, int y){
        selectedDateTxt.setText("Date: " + d +"/" + m +"/" + y);
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        if (id == 111) {
            return new DatePickerDialog(this, dateLPickerListener, year, month, day);
        }
        return null;
    }

    private DatePickerDialog.OnDateSetListener dateLPickerListener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker arg0, int y, int m, int d) {
            displayDate(d, m+1, y);
        }
    };
}

 

Screenshots

Android Date Picker Example

Android Date Picker Example

Comment below if you are facing any problem in above android date picker example.

3 thoughts on “Android Date Picker Example”

  1. Hi.your articles are very helpful.I would like to know how to make a javascript function to show date when a button is clicked.The same function hides the date after the second click.I will appreciate your assistance.Thank you

Leave a Comment

Your email address will not be published. Required fields are marked *