What is datepicker

Android datepicker is used to pick the date.


Date Picker:.

In this section i am going to show the datepicker with practical example.

First open the android studio and create a project and give the name of your project like DatePickerDemo.

The screenshots of the program is as follows:

Smiley face Smiley face

Drag and drop the one TextView from pallet. Open the activity_main.xml file and code look like this


<android.support.constraint.ConstraintLayout 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="wrap_content"
    android:layout_height="wrap_content"
    tools:context=".MainActivity">


    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Select Date"
        android:layout_marginTop="100dp"
        android:layout_marginStart="100dp"
        android:textSize="30sp"/>
</android.support.constraint.ConstraintLayout>


       

Open the MainActivity.java file and write code look like this.


package com.example.niitpc.myapplication;

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.util.Log;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity {

    TextView textView;
    
    DatePickerDialog.OnDateSetListener  mOnDateSetListener;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    textView=(TextView)findViewById(R.id.textView);
    textView.setOnClickListener(
    new View.OnClickListener() {
    @Override
    public void onClick(View v) {

    Calendar cal=Calendar.getInstance();
    int year=cal.get(Calendar.YEAR);
    int month=cal.get(Calendar.MONTH);
    int day=cal.get(Calendar.DAY_OF_MONTH);
    
    DatePickerDialog dialog=new DatePickerDialog(MainActivity.this, 
    android.R.style.Theme_Holo_Light_Dialog_MinWidth,mOnDateSetListener,year,month,day);
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    dialog.show();

    }
   }
  );
    mOnDateSetListener=new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {

    month=month+1;
    String date=month+"/"+dayOfMonth+"/"+year;
    textView.setText(date);
    }
  };

}
}


       

Explanation:

TextView textView:This statement create the object textView of TextView.

DatePickerDialog.OnDateSetListener mOnDateSetListener;:Here we declare the mOnDateSetListener of DatePickerDialog.

textView=(TextView)findViewById(R.id.textView);:This statement initialized object textView.

textView.setOnClickListener:Here we have to create the listener for textView.

Calendar cal=Calendar.getInstance():Calendar getInstance method returns a Calendar object whose calendar fields have been initialized with the current date and time.

int year=cal.get(Calendar.YEAR):This statement store the year in year variable.

int month=cal.get(Calendar.YEAR):This statement store the month in month variable.

int day=cal.get(Calendar.YEAR):This statement store the day of month in day variable.

int year=cal.get(Calendar.YEAR):This statement store the year in year variable.

DatePickerDialog dialog=new DatePickerDialog(MainActivity.this, android.R.style.Theme_Holo_Light_Dialog_MinWidth,mOnDateSetListener,year,month,day); dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); dialog.show(). This statement create the datepicker dialog with transparent background.

mOnDateSetListener=new DatePickerDialog.OnDateSetListener():This statement instantiate mOnDateSetListener and it will create the onDateSet().Which take the four argument the second is year,third is month,fourth is dayofmonth.

month=month+1:month always start with 0 so we need to add one.

String date=month+"/"+dayOfMonth+"/"+year:This statement concat the month,dayofMonth and year and store into date variable.

textView.setText(date):This statement set the date into textView.


DatePicker

What is datepicker for video Click here

Smiley face                     Smiley face