Android CalendarView Using Kotlin With Example

In this article, we will learn about android CalendarView using Kotlin. We will go through various example that demonstrates how to use different attributes of CalendarView. For example,

In this article, we will get answer to questions like –

  • What is CalendarView?
  • Why should we consider CalendarView while designing ui for any app?
  • What are possibilities using CalendarView while designing ui? etc.

Let’s have a quick demo of things we want to cover in this tutorial –

Output

Tutorialwing Kotlin CalendarView Output Android CalendarView Using Kotlin With Example

Tutorialwing Kotlin CalendarView Output

Getting Started

We can define android CalendarView widget as below –

CalendarView is a widget that are used to display and select date.

Now, how do we use CalendarView in android application ?

Creating New Project

At first, we will create an application.
So, follow steps below to create any android project in Kotlin –

Step Description
1. Open Android Studio (Ignore if already done).
2. Go to File => New => New Project. This will open a new window. Then, under Phone and Tablet section, select Empty Activity. Then, click Next.
3. In next screen, select project name as CalendarView. Then, fill other required details.
4. Then, clicking on Finish button creates new project.

Newbie in Android ?

Some very important concepts (Recommended to learn before you move ahead)

Before we move ahead, we need to setup for viewBinding to access Android CalendarView Using Kotlin file without using findViewById() method.

Setup ViewBinding

Add viewBinding true in app/build.gradle file.

 
 android { 
 	// OTHER CODE... 
 	buildFeatures { 
 		viewBinding true 
 	} 
 } 
 

Now, set content in activity using view binding.
Open MainActivity.kt file and write below code in it.

 
 class MainActivity : AppCompatActivity() { 
 	
 	private lateinit var binding: ActivityMainBinding 
 	
 	override fun onCreate(savedInstanceState: Bundle?) { 
 		super.onCreate(savedInstanceState) 
 		binding = ActivityMainBinding.inflate(layoutInflater) 
 		val view = binding.root 
 		setContentView(view) 
 	} 
 } 
 

Now, we can access view in Kotlin file without using findViewById() method.

Using CalendarView in Kotlin

Follow steps below to use CalendarView in newly created project –

  • Open res/values/strings.xml file. Then, add below code into it.
    <resources>
        <string name="app_name">CalendarView</string>
    </resources>
    
  • Open res/layout/activity_main.xml file. Then, add below code in it –
    <?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"
        tools:context=".MainActivity">
    
        <CalendarView
            android:id="@+id/calendarView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    
  • We can also access it in Kotlin File, MainActivity.kt, as below –

    package com.tutorialwing.calendarview
    
    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import android.widget.Toast
    import com.tutorialwing.calendarview.databinding.ActivityMainBinding
    
    class MainActivity : AppCompatActivity() {
    
    	private lateinit var binding: ActivityMainBinding
    
    	override fun onCreate(savedInstanceState: Bundle?) {
    		super.onCreate(savedInstanceState)
    		binding = ActivityMainBinding.inflate(layoutInflater)
    		val view = binding.root
    		setContentView(view)
    
    		setupCalendar()
    	}
    
    	private fun setupCalendar() {
    		binding.calendarView.setOnDateChangeListener { view, year, month, dayOfMonth ->
    			// Note that months are indexed from 0. So, 0 means January, 1 means february, 2 means march etc.
    			val msg = "Selected date is " + dayOfMonth + "/" + (month + 1) + "/" + year
    			Toast.makeText(this@MainActivity, msg, Toast.LENGTH_SHORT).show()
    		}
    	}
    }
    

    We have accessed calendarView using kotlin file (i.e. MainActivity.kt file) in the application. Then, we have set date change listener to display selected date as toast message in the application.

Now, run the application. We will get output as below –

Tutorialwing Kotlin CalendarView Output Android CalendarView Using Kotlin With Example

Tutorialwing Kotlin CalendarView Output

Different Attributes of CalendarView in XML

Now, we will see how to use different attributes of Android CalendarView using Kotlin to customise it –

Set Id of CalendarView

Many a time, we need id of View to access it in kotlin file or create ui relative to that view in xml file. So, we can set id of CalendarView using android:id attribute like below –

    <CalendarView
        android:id="@+id/calendarView_ID"
        />

Here, we have set id of CalendarView as calendarView_ID using android:id=”” attribute. So, if we need to reference this CalendarView, we need to use this id – calendarView_ID.
Learn to Set ID of CalendarView Dynamically

Set Width of CalendarView

We use android:layout_width=”” attribute to set width of CalendarView.
We can do it as below –

    <CalendarView
        android:id="@+id/calendarView_ID"
        android:layout_width="wrap_content"
        />

Width can be either “MATCH_PARENT” or “WRAP_CONTENT” or any fixed value (like 20dp, 30dp etc.).
Learn to Set Width of CalendarView Dynamically

Set Height of CalendarView

We use android:layout_height=”” attribute to set height of CalendarView.
We can do it as below –

    <CalendarView
        android:id="@+id/calendarView_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

Height can be either “MATCH_PARENT” or “WRAP_CONTENT” or any fixed value.
Learn to Set Height of CalendarView Dynamically

Set Padding of CalendarView

We use android:padding=”” attribute to set padding of CalendarView.
We can do it as below –

    <CalendarView
        android:id="@+id/calendarView_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        />

Here, we have set padding of 10dp in CalendarView using android:padding=”” attribute.
Learn to Set Padding of CalendarView Dynamically

Set Margin of CalendarView

We use android:layout_margin=”” attribute to set margin of CalendarView.
We can do it as below –

    <CalendarView
        android:id="@+id/calendarView_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        />

Here, we have set margin of 10dp in CalendarView using android:layout_margin=”” attribute.
Learn to Set Margin of CalendarView Dynamically

Set Background of CalendarView

We use android:background=”” attribute to set background of CalendarView.
We can do it as below –

    <CalendarView
        android:id="@+id/calendarView_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ff0000"
        />

Here, we have set background of color #ff0000 in CalendarView using android:background=”” attribute.
Learn to Set Background of CalendarView Dynamically

Set Visibility of CalendarView

We use android:visibility=”” attribute to set visibility of CalendarView.
We can do it as below –

    <CalendarView
        android:id="@+id/calendarView_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"
        />

Here, we have set visibility of CalendarView using android:visiblity=”” attribute. Visibility can be of three types – gone, visible and invisible
Learn to Set Visibility of CalendarView Dynamically

Till now, we have see how to use android CalendarView using Kotlin. We have also gone through different attributes of CalendarView to perform certain task. Let’s have a look at list of such attributes and it’s related task.

Different Attributes of Android CalendarView Widget

Below are the various attributes that are used to customise android CalendarView Widget. However, you can check the complete list of attributes of CalendarView in it’s official documentation site. Here, we are going to list some of the important attributes of this widget –

Some of the popular attributes of android CalendarView widget are –

Sr. XML Attributes Description
1 android:dateTextAppearance The text appearance for the day numbers in the calendar grid
2 android:firstDayOfWeek Specifies first day of the calendar
3 android:maxDate Specifies maximal date shown in calendar view in mm/dd/yyyy format
4 android:minDate Specifies minimal date shown in calendar view in mm/dd/yyyy format

Some of the popular attributes of android calendarView widget inherited from FrameLayout are –

Sr. XML Attributes Description
1 android:foregroundGravity Specifies the gravity to apply to the foreground drawable
2 android:measureAllChildren Specifies whether to measure all children or only those in VISIBLE or INVISIBLE state when measuring

Some of the popular attributes of android calendarView inherited from viewGroup are –

Sr. XML Attributes Description
1 android:animateLayoutChanges Defines whether to run layout transition when there is any change in layout
2 android:animationCache Defines whether to create drawing cache for children by layout animation
3 android:clipToPadding Defines whether the ViewGroup will clip its children and resize (but not clip) any EdgeEffect to its padding, if padding is not zero
4 android:layoutAnimation Defines the layout animation to be used when the viewGroup is laid out for the first time
5 android:layoutMode Specifies the layout mode of the viewGroup

Some of the popular attributes of android calendarView inherited from View are –

Sr. XML Attributes Description
1 android:alpha Defines alpha to the view
2 android:background Defines drawable to the background
3 android:backgroundTint Defines tint to apply to the background
4 android:clickable Defines whether the view is clickable or not
5 android:elevation Defines elevation of the view
6 android:focusable Defines whether this view can take focus or not
7 android:id Defines id of the view
8 android:visibility Defines the visibility(VISIBLE, INVISIBLE, GONE) of the view

We have seen different attributes of CalendarView and how to use it. If you wish to visit post to learn more about it

Thus, we have seen what is CalendarView, how can we use android CalendarView using Kotlin ? etc. We also went through different attributes of android CalendarView.

Leave a Reply