Tuesday, June 9, 2020

android kotlin - CalendarView get selected date

MainActivity.kt

package com.cfsuman.kotlintutorials

import android.app.Activity
import android.graphics.Color
import android.os.Bundle
import android.widget.*
import java.text.DateFormat
import java.util.*


class MainActivity : Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // get the widgets reference from XML layout
        val calendarView = findViewById<CalendarView>(R.id.calendarView)
        val button = findViewById<Button>(R.id.button)
        val textView = findViewById<TextView>(R.id.textView)


        // get a calendar instance
        val calendar = Calendar.getInstance()


        // calendar view date change listener
        calendarView.setOnDateChangeListener {
                view, year, month, dayOfMonth ->
            // set the calendar date as calendar view selected date
            calendar.set(year,month,dayOfMonth)

            // set this date as calendar view selected date
            calendarView.date = calendar.timeInMillis

            // format the calendar view selected date
            val dateFormatter = DateFormat
                .getDateInstance(DateFormat.MEDIUM)
            textView.text = "Date changed "
            textView.append(dateFormatter.format(calendar.time))
            textView.setTextColor(Color.BLUE)
        }


        // button click listener
        button.setOnClickListener {
            // get calendar view selected date
            val selectedDate:Long = calendarView.date

            // set the calendar date as calendar view selected date
            calendar.timeInMillis = selectedDate

            // format the calendar view selected date
            val dateFormatter = DateFormat
                .getDateInstance(DateFormat.MEDIUM)
            textView.text = "Selected date "
            textView.append(dateFormatter.format(calendar.time))
            textView.setTextColor(Color.DKGRAY)
        }
    }
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/rootLayout"
    android:background="#DCDCDC"
    android:padding="24dp">

    <CalendarView
        android:id="@+id/calendarView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="Get Selected Date"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/calendarView" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:fontFamily="sans-serif"
        android:textSize="24sp"
        tools:text="TextView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button" />

</androidx.constraintlayout.widget.ConstraintLayout>