Dynamic DatePicker in Kotlin

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 the users will select a valid date.

In android DatePicker having two modes, first one to show the complete calendar and second one shows the dates in spinner view.

We can create a DatePicker control in two ways either manually in XML file or create it in Activity file programmatically.

First we create a new project by following the below steps:

  1. Click on File, then New => New Project.
  2. After that include the Kotlin support and click on next.
  3. Select the minimum SDK as per convenience and click next button.
  4. Then select the Empty activity => next => finish.

Modify activity_main.xml file




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/linear_layout"
    android:gravity = "center">
      
</LinearLayout>


Modify strings.xml file




<resources>
    <string name="app_name">DynamicDatePickerInKotlin</string>
</resources>


Create the DatePicker in MainActivity.kt file

First of all, we declare a variable datePicker to create the DatePicker widget in Kotlin file.

val datePicker = DatePicker(this)

then, we will add the datePicker in the linearLayout using

val linearLayout = findViewById(R.id.linear_layout)
        // add datePicker in LinearLayout
        linearLayout?.addView(datePicker)

We are familiar with further activities in previous articles.




package com.w3wiki.myfirstkotlinapp
  
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.ViewGroup
import android.widget.*
import java.util.*
  
class MainActivity : AppCompatActivity() {
  
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        val datePicker = DatePicker(this)
        val layoutParams = LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT)
        datePicker.layoutParams = layoutParams
  
  
        val linearLayout = findViewById<LinearLayout>(R.id.linear_layout)
        // add datePicker in LinearLayout
        linearLayout?.addView(datePicker)
  
        val today = Calendar.getInstance()
        datePicker.init(today.get(Calendar.YEAR), today.get(Calendar.MONTH),
            today.get(Calendar.DAY_OF_MONTH)
  
        ) { view, year, month, day ->
            val month = month + 1
            val msg = "You Selected: $day/$month/$year"
            Toast.makeText(this@MainActivity, msg, Toast.LENGTH_SHORT).show()
  
        }
    }
}


AndroidManifest.xml file




<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.w3wiki.myfirstkotlinapp">
  
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
  
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
  
</manifest>


Run as Emulator:

 



Contact Us