Date Picker

Date Picker

DatePicker dialog is seen used in many android applications where we have to select the date. This widget is mostly seen in the hotel reservation applications for travel booking applications. With the help of this widget, we can simply pick the date from the DatePicker dialog. In this article, we will take a look at How to implement Date Picker in Android. A sample video is given below to get an idea about what we are going to do in this article.
 
  • XML
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/idRLContainer" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <TextView android:id="@+id/idTVSelectedDate" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@id/idBtnPickDate" android:layout_centerInParent="true" android:layout_margin="20dp" android:gravity="center" android:padding="10dp" android:text="Date" android:textAlignment="center" android:textColor="@color/black" android:textSize="20sp" android:textStyle="bold" /> <!--on below line we are creating a button for date picker--> <Button android:id="@+id/idBtnPickDate" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_margin="20dp" android:text="Pick Date" android:textAllCaps="false" /> </RelativeLayout>
 
  • Kotlin
package com.appdevelopers.kotlinproject import android.app.DatePickerDialog import android.os.Bundle import android.widget.Button import android.widget.TextView import androidx.appcompat.app.AppCompatActivity import java.util.* class MainActivity : AppCompatActivity() { lateinit var pickDateBtn: Button lateinit var selectedDateTV: TextView override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) pickDateBtn = findViewById(R.id.idBtnPickDate) selectedDateTV = findViewById(R.id.idTVSelectedDate) // on below line we are adding click listener for our button pickDateBtn.setOnClickListener { // on below line we are getting the instance of our calendar. val c = Calendar.getInstance() // on below line we are getting our day, month and year. val year = c.get(Calendar.YEAR) val month = c.get(Calendar.MONTH) val day = c.get(Calendar.DAY_OF_MONTH) // on below line we are creating a variable for date picker dialog. val datePickerDialog = DatePickerDialog( // on below line we are passing context. this, { view, year, monthOfYear, dayOfMonth -> // on below line we are setting date to our text view. selectedDateTV.text = (dayOfMonth.toString() + "-" + (monthOfYear + 1) + "-" + year) }, year, month, day ) // on below line we are passing year, month // and day for the selected date in our date picker. datePickerDialog.show() } // at last we are calling show // to display our date picker dialog. } }