Android CalendarView Tutorial With Example

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

Output

Tutorialwing Android calendarView Output Android Calendar Tutorial with example

Tutorialwing Android calendarView Output

Getting Started

CalendarView widget can be defined as below –

CalendarView is a widget that are used to display and select dates. However, you can configure range of dates to be shown in the calendar.

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 Defines first day of the calendar
3 android:maxDate Defines maximal date shown in calendar view in mm/dd/yyyy format
4 android:minDate Defines 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 Defines the gravity to apply to the foreground drawable
2 android:measureAllChildren Defines 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 Specifies whether to run layout transition when there is any change in layout
2 android:animationCache Specifies 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 Specifies the layout animation to be used when the viewGroup is laid out for the first time
5 android:layoutMode Defines 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 Sets alpha to the view
2 android:background Sets drawable to the background
3 android:backgroundTint Sets tint to apply to the background
4 android:clickable Specifies whether the view is clickable or not
5 android:elevation Sets elevation of the view
6 android:focusable Specifies whether this view can take focus or not
7 android:id Specifies id of the view
8 android:visibility Specifies the visibility(VISIBLE, INVISIBLE, GONE) of the view



Example of Android CalendarView Widget

At first, we will create android application. Then, we will use calendarView widget in this application.

1. Creating New Project

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

Step Description
1. Open Android Studio.
2. Go to File => New => New Project. Write application name as CalendarView. Then, 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. If you have followed above process correctly, you will get a newly created project successfully. However, you can also visit post to create a new project to know steps in detail.

Now, we will modify xml and java file to use calendarView widget 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 res/layout/activity_main.xml file. Then, 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 widget in java file to perform some operations on it.

4. Access CalendarView Widget in java file

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

package com.tutorialwing.calendarview;

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

public class MainActivity extends AppCompatActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		CalendarView calendarView = findViewById(R.id.calendarView);
		if (calendarView != null) {
			calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
				@Override
				public void onSelectedDayChange(@NonNull CalendarView view, int year, int month, int dayOfMonth) {
					// Note that months are indexed from 0. So, 0 means January, 1 means february, 2 means march etc.
					String msg = "Selected date is " + dayOfMonth + "/" + (month + 1) + "/" + year;
					Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
				}
			});
		}
	}
}

In MainActivity.java file, we have accessed calendarView widget. Then, we have set a date change listener that shows selected date as toast message.

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

AndroidManifest.xml

Code inside src/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 tutorial on Android CalendarView widget.

Leave a Reply