Open In App

Android | Creating a Calendar View app

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.
 




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
    <!-- Add TextView to display the date -->
    <TextView
        android:id="@+id/date_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="150dp"
        android:layout_marginTop="20dp"
        android:text="Set the Date"
        android:textColor="@android:color/background_dark"
        android:textStyle="bold" />
  
    <!-- Add CalendarView to display the Calendar -->
    <CalendarView
        android:id="@+id/calendar"
        android:layout_marginTop="80dp"
        android:layout_marginLeft="19dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </CalendarView>
  
</RelativeLayout>




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 {
  
    // Define the variable of CalendarView type
    // and TextView type;
    CalendarView calendar;
    TextView date_view;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // By ID we can use each component
        // which id is assign in xml file
        // use findViewById() to get the
        // CalendarView and TextView
        calendar = (CalendarView)
            findViewById(R.id.calendar);
        date_view = (TextView)
            findViewById(R.id.date_view);
  
        // Add Listener in calendar
        calendar
            .setOnDateChangeListener(
                new CalendarView
                    .OnDateChangeListener() {
                        @Override
  
                        // 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
                            date_view.setText(Date);
                        }
                    });
    }
}

Output: 
 


Article Tags :