Monday, June 8, 2020

android kotlin - CalendarView set date

MainActivity.kt

package com.cfsuman.kotlintutorials

import android.app.Activity
import android.os.Bundle
import android.widget.*
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 button2 = findViewById<Button>(R.id.button2)


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


        // first button click listener
        button.setOnClickListener {
            // set the calendar date to 31 march 1951
            calendar.set(
                // int: the value used to set the YEAR calendar field.
                1951, // year

                // int: the value used to set the MONTH calendar field.
                // Month value is 0-based. e.g., 0 for January.
                2, // month

                // int: the value used to set the
                // DAY_OF_MONTH calendar field.
                31 // day of month
            )

            // finally, set this date to calendar view
            calendarView.date = calendar.timeInMillis
        }


        // second button click listener
        button2.setOnClickListener {
            // set the calendar date as 3 nov 2025
            calendar.set(
                2025, // year
                10, // month nov, 0 based index
                3 // day of month
            )

            // finally, apply the date to calendar view
            calendarView.setDate(
                calendar.timeInMillis, // long: The date.

                // boolean: Whether to animate the scroll
                // to the current date.
                false,

                // boolean: Whether to center the current date
                // even if it is already visible.
                false
            )
        }
    }
}
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"
    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="16dp"
        android:text="Set 31 Mar 1951"
        app:layout_constraintEnd_toStartOf="@+id/button2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/calendarView" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Set 3 Nov 2025"
        app:layout_constraintBottom_toBottomOf="@+id/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/button" />

</androidx.constraintlayout.widget.ConstraintLayout>