Android DatePicker is a user interface control which is used to select the date by day, month and year in our Android application. DatePicker is used to ensure that users select a valid date.
In Android DatePicker has two modes, the first one shows the complete calendar and the second one shows the dates in spinner view. We can create a DatePicker control in two ways either manually in an XML file or create it in an Activity file programmatically. In this article, we will be learning how to implement a Date Picker programmatically in Kotlin.
Step by Step Implementation
Step 1: Create a new Android Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.
Note: Select Kotlin as the programming language.
Step 2: Modify activity_main.xml file
In this file, we use the LinearLayout, which is going to be accessed in the Kotlin file. Also set the attribute of the Layout like id, orientation and gravity etc.
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
</LinearLayout>
Step 3: Create the DatePicker in MainActivity.kt file
First of all, we create a textview and the DatePicker widget.
val textView = TextView(this)
val datePicker = DatePicker(this)
we define a layout parameter for both the views and set them to the views
val layoutParams = LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
layoutParams.setMargins(100)
datePicker.layoutParams = layoutParams
textView.layoutParams = layoutParams
then, we define the main layout and the two views to the layout
val linearLayout: LinearLayout = findViewById(R.id.main)
linearLayout.addView(datePicker)
linearLayout.addView(textView)
then, we define and get the current date, month and year
val today = Calendar.getInstance()
val currentDate = today.get(Calendar.DAY_OF_MONTH)
val currentMonth = today.get(Calendar.MONTH)
val currentYear = today.get(Calendar.YEAR)
Finally, we initialize the datepicker with the current date
datePicker.init(currentYear, currentMonth, currentDate) { view, year, month, day ->
val month1 = month + 1
val message = "$day - $month1 - $year"
textView.text = message
}
MainActivity.kt:
package org.geeksforgeeks.demo
import android.os.Bundle
import android.view.ViewGroup
import android.widget.DatePicker
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.setMargins
import java.util.Calendar
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// create textView programmatically
val textView = TextView(this)
// create DatePicker programmatically
val datePicker = DatePicker(this)
val layoutParams = LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
layoutParams.setMargins(100)
datePicker.layoutParams = layoutParams
textView.layoutParams = layoutParams
textView.textSize = 24f
val linearLayout: LinearLayout = findViewById(R.id.main)
linearLayout.addView(datePicker)
linearLayout.addView(textView)
val today = Calendar.getInstance()
val currentDate = today.get(Calendar.DAY_OF_MONTH)
val currentMonth = today.get(Calendar.MONTH)
val currentYear = today.get(Calendar.YEAR)
datePicker.init(currentYear, currentMonth, currentDate) { _, year, month, day ->
val month1 = month + 1
val message = "$day - $month1 - $year"
textView.text = message
}
}
}