Android CalendarView Using Kotlin With Example

Greetings!
We have recently published 100+ articles on android tutorials with kotlin and java. If you need, you may visit Android Tutorial for beginners page. You can also check Kotlin Tutorial for beginners. Also, if you are interested in content writing, you can mail us at tutorialwing@gmail.com.

Hello Readers! In this post, we are going to learn about android calendarView using kotlin in any android application. We will also learn about different attributes of android calendarView that can be used to customise this widget.

Output

Tutorialwing Kotlin CalendarView Output Android CalendarView Using Kotlin With Example

Tutorialwing Kotlin CalendarView Output

Getting Started

Android CalendarView can be defined as below –

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

Different Attributes of Android CalendarView 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



Example of Android CalendarView Using Kotlin

At first, we will create android application. Then, we will use calendarView using kotlin in the application.

1. Creating New Project in Kotlin

Follow steps below to create new project. Please ignore the steps if you have already created the project.

Step Description
1. Open Android Studio.
2. Go to File => New => New Project. Write application name as CalendarView. Then, check Include Kotlin Support and click next button.
3. Select minimum SDK you need. However, we have selected 17 as minimum SDK. Then, click next button
4. Then, select Empty Activity => click next => click finish.
5. You will get a newly created project successfully if you have followed steps properly.

Since we have a project now, we will modify xml and other files to use calendarView using kotlin in the application.

2. Modify values folder

No values folders have been modified. So, we are not going to mention them here.

3. Use CalendarView Widget in xml file

Open src/main/res/layout/activity_main.xml file and add below code into it.

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

	<CalendarView
		android:id="@+id/calendarView"
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
		android:layout_marginBottom="50dp"
		android:layout_marginLeft="50dp"
		android:layout_marginRight="50dp"/>

</LinearLayout>

In activity_main.xml file, we have defined calendarView widget. Now, we will access this calendarView using kotlin file to perform some operations on it.

4. Access CalendarView Widget in Kotlin file

Open src/main/java/com.tutorialwing.calendarview/MainActivity.kt file and add below code into it.

package com.tutorialwing.calendarview

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.CalendarView
import android.widget.Toast

class MainActivity : AppCompatActivity() {

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

        val calendarView = findViewById<CalendarView>(R.id.calendarView)
        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.

Since AndroidManifest.xml file is very important in any android application, we are also going to see the content inside this file.

AndroidManifest.xml file

Code inside main/AndroidManifest.xml file is as below.

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.tutorialwing.calendarview"
		  xmlns:android="http://schemas.android.com/apk/res/android">

	<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>

When we run the program, we will get output as shown above.

That’s end of our tutorial on Android calendarView using Kotlin.

Support Us

If you like Tutorialwing and would like to contribute, you can email an article on any educational topic at tutorialwing@gmail.com. We would love to publish your article. See your article on Tutorialwing and help others with your knowledge. Follow Facebook, LinkedIn, Google+, Twitter, Youtube for latest updates.
Greetings!
We have recently published 100+ articles on android tutorials with kotlin and java. If you need, you may visit Android Tutorial for beginners page. You can also check Kotlin Tutorial for beginners