Open In App
Related Articles

Android | Creating a Calendar View app

Like Article
Save Article
Report issue

This article shows how to create an android application for displaying the Calendar using CalendarView. It also provides the selection of the current date and displaying the date. The setOnDateChangeListener Interface is used which provide onSelectedDayChange method.

  1. onSelectedDayChange: In this method, we get the values of days, months, and years that are selected by the user.

Below are the steps for creating the Android Application of the Calendar.

  • Step 1: Create a new project and you will have a layout XML file and java file. Your screen will look like the image below.
  • Step 2: Open your xml file and add CalendarView and TextView. And assign id to TextView and CalendarView. After completing this process, the xml file screen looks like given below.
  • Step 3: Now, open up the activity java file and define the CalendarView and TextView type variable, and also use findViewById() to get the Calendarview and textview.
  • Step 4: Now, add setOnDateChangeListener interface in object of CalendarView which provides setOnDateChangeListener method. In this method, we get the Dates(days, months, years) and set the dates in TextView for Display. 
  • Step 5: Now run the app and set the current date which will be shown on the top of the screen.
    Complete code of or activity_main.xml of Calendar is given below.


<?xml version="1.0" encoding="utf-8"?>
    <!-- Add TextView to display the date -->
        android:text="Set the Date"
        android:textStyle="bold" />
    <!-- Add CalendarView to display the Calendar -->


package org.geeksforgeeks.navedmalik.calendar;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
    // Define the variable of CalendarView type
    // and TextView type;
    CalendarView calendar;
    TextView date_view;
    protected void onCreate(Bundle savedInstanceState)
        // By ID we can use each component
        // which id is assign in xml file
        // use findViewById() to get the
        // CalendarView and TextView
        calendar = (CalendarView)
        date_view = (TextView)
        // Add Listener in calendar
                new CalendarView
                    .OnDateChangeListener() {
                        // In this Listener have one method
                        // and in this method we will
                        // get the value of DAYS, MONTH, YEARS
                        public void onSelectedDayChange(
                            @NonNull CalendarView view,
                            int year,
                            int month,
                            int dayOfMonth)
                            // Store the value of date with
                            // format in String type Variable
                            // Add 1 in month because month
                            // index is start with 0
                            String Date
                                = dayOfMonth + "-"
                                  + (month + 1) + "-" + year;
                            // set this date in TextView for Display



Last Updated : 26 Jul, 2021
Like Article
Save Article
Share your thoughts in the comments
Similar Reads