Creating a Calendar View App in Android

This article demonstrates how to make an Android application that uses CalendarView to display the Calendar. It also provides the option to pick the current date and view the date. The onSelectedDayChange method is provided by the setOnDateChangeListener Interface.

onSelectedDayChange Mthod

With this method, we obtain the user-selected values for the days, months, and years.

Step 1: You will have a layout XML file and java file when you create a new project.

Step 2: Add the CalendarView and TextView by opening your xml file then give TextView and CalendarView IDs. Following this procedure, the xml file screen appears as seen below.

Step 3: Open the activity.java file and declare the type variables for the CalendarView and TextView, then use findViewById() to retrieve them.

Step 4: Add the setOnDateChangeListener interface, which is available in the CalendarView object's setOnDateChangeListener method, now. This method retrieves the dates (days, months, and years) and sets the dates for display in the TextView.

Step 5: The current date will be displayed at the top of the screen after the app has been launched.

XML Code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
	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">
	<!-- Include a TextView to show the date -->
	<TextView
		android:id="@+id/date_view1"
		android:layout_width="150dp"
		android:layout_height="50dp"
		android:layout_alignParentStart="true"
		android:layout_alignParentEnd="true"
		android:layout_marginStart="133dp"
		android:layout_marginLeft="170dp"
		android:layout_marginTop="25dp"
		android:layout_marginEnd="128dp"
		android:text="Please pick a date"
		android:textColor="@android:color/background_dark"
		android:textStyle="bold" />


	<!-- Add CalendarView to display the Calendar -->
	<CalendarView
		android:id="@+id/calendar1"
		android:layout_width="300dp"
		android:layout_height="300dp"
		android:layout_alignParentStart="true"
		android:layout_alignParentEnd="true"
		android:layout_marginStart="49dp"
		android:layout_marginLeft="22dp"
		android:layout_marginTop="100dp"
		android:layout_marginEnd="62dp">
	</CalendarView>
</RelativeLayout>

Java Code:

package org.geeksforgeeks.navedmalik.calendar;


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


public class MainActivity extends AppCompatActivity {


	// Set the variables for the TextView
	// and CalendarView types
	CalendarView calendar1;
	TextView date_view1;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		// By ID we can use each componentwhich id is assign in xml file
		// which id is assign in xml file
		// CalendarView and TextView can be 
		// obtained using findViewById()
		Calendar1 = (CalendarView)
			findViewById(R.id.calendar1);
		date_view1 = (TextView)
			findViewById(R.id.date_view1);


		// Calendar Add Listener
		Calendar1.setOnDateChangeListener(new CalendarView
				.OnDateChangeListener() {
					@Override
					// We will obtain the value of 
					// the days, months, and years
					// using the method in this listener
					public void onSelectedDayChange(
						@NonNullCalendarView view,
						int year1,
						int month1,
						int dayOfMonth1)
					{
			// Store the formatted value of the date
			// in a variable of type String
			// Because month indexes begin with
			// 0, add 1 to the month
						String Date1
							= dayOfMonth1 + "-"
							+ (month1 + 1) + "-" + year1;
		// in TextView, enter this date for display
						date_view1.setText(Date1);
	}
	});
	}
}

Output:

Creating a Calendar View App in Android

Related Topics

Android RecyclerView

RecyclerView is a ViewGroup brought to AndroidStudio as successor to ListView and GridView. These are all improvements and are included in the latest v7 support package. It was written to...

8 minutes read.

How to Generate QRCode in Android?

Many apps use QR codes to show data in a way that is machine-readable. Data is represented using these codes in a secure manner such that only computers, not humans,...

4 minutes read.

Android Navigation Drawer

A sliding menu used to show important links in the Android Application is called Navigation Drawer. The navigation drawermakes it easier to navigate between the fragments. It is not displayed...

5 minutes read.

What is Android Studio

Android Studio powered by the IntelliJ platform is an integrated development environment (IDE) and is officially recognized for developing androidapplications. Its foundation lies on JetBrains' IntelliJ IDEA and is specialized...

3 minutes read.

Android Studio's Layout Editor

The Layout Editor allows you to efficiently create layouts by dragging UI elements from the Palette into the visual design editor instead of manually creating the layout XML file by...

8 minutes read.

Assets folder in Android

You might have noticed that unlike the Eclipse ADT (App Development Tools), the Android Studio does not have an Assets folder, which is mainly used to store the web resources...

3 minutes read.

Android EditText

In Android, EditText is a UI widget utilized to allow the user to input or change the text. While using the EditTextwidget in your Android application, you need to use...

4 minutes read.

Top Programming Languages for Android Development

For mobile apps, there are so many programming languages. You would need to spend days simply researching if you had to pick one from a list of 30 coding languages...

8 minutes read.

Android Activity

What is an Activity? Android Activity is a screen in the user interface of an Android application. In this way, Android activity is very similar to the desktop application window. An...

4 minutes read.

Creating a Calendar View App in Android

This article demonstrates how to make an Android application that uses CalendarView to display the Calendar. It also provides the option to pick the current date and view the date....

3 minutes read.

Android GridView

Theview group that displays items in a two-dimensional scrolling grid is called GridView. The data isadded into this grid layout from anArray List or database. The adapter class isutilized to...

4 minutes read.

Android Broadcast Receivers

Android applications can send or receive broadcast messages from the Android operating system and other Androidapplications, like the publish-subscribe design pattern. When an event of interest happens,these broadcasts are sent. For...

4 minutes read.

Content Providers of Android with Example

An essential part of Android that performs the function of a relational database by storing application data is called a content provider. The function of the content provider in the...

13 minutes read.

Android 9.0 Pie vs. Android 10, which one is better

Android smartphone users are always searching for new system upgrades and keep an eye on system updates whenever the next version is published. Android 9 Pie and Android Q 10 are...

5 minutes read.

Android Emulator

A tool for creating Android virtual devices (including software and virtual hardware) on your personal desktop is known as Emulator. We can use these virtual devices as the target device...

4 minutes read.

Android ListView

A kind of AdapterView that displays a vertical list of scrollable views, each of which is laid out below each other is called ListView. Using adapters, items are inserted into...

4 minutes read.

Android Time picker example

How to pick Time from the user in android? Write an android example to pick Time from the user and display the date to the user. Android provides controls for the user...

3 minutes read.

Jetpack Architecture Components in Android

A collection of software elements, libraries, tools, and instructions is called Android Jetpack. It is available to assist in creating reliable Android applications. Jetpack, which was introduced by Google in...

8 minutes read.

Android TextView

Widgets are user interface (UI) elements that help users to interact with Android applications. It is one of many equipments that can be used to improvise the user interface of...

4 minutes read.

Android Studio Resources

To build an outstanding Android application there are many items that you need to use. Aside from the coding of your application, you need to handle various resources such as...

5 minutes read.