Open In App

Android – onDateChangeListener in CalendarView

Last Updated : 06 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In our day-to-day life, we are using android applications many times, many applications use calendars somehow, for creating a schedule or for creating reminders or date-of-birth chosen in forms. Sometimes we select the date but forgot to confirm it, because of this problem user sets the wrong date. So, we know that it is very important to confirm whether the date is changed or not in the calendar, for solving this problem we will use the onDateChangeListener method from CalendarView in android. A sample video is given below to get an idea about what we are going to do in this article.

What is onDateChangeListener?

It is a method that is called when the date is changed inside CalendarView. 

Note: This Android article covered in both Java and Kotlin languages. 

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.

Step 2: Working with the activity_main.xml file

Navigate to app > res > layout > activity_main.xml and add the below code to it. Comments are added in the code to get to know in detail. 

XML




<!-- XML code -->
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    tools:context=".MainActivity">
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
 
        <!-- TextView to update the chosen date -->
        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Date"
            android:textColor="#4CAF50"
            android:textSize="30sp"
            android:textAlignment="center"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="10dp"/>
  
          <!-- CalenderView to choose date -->
        <CalendarView
            android:id="@+id/calendarView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="#4CAF50"
            android:calendarTextColor="@color/white"/>
       
    </LinearLayout>
   
</androidx.constraintlayout.widget.ConstraintLayout>


Step 3: Working with the MainActivity file 

Navigate to app > java > your app’s package name > MainActivity file and add the below code to it. Comments are added in the code to get to know in detail. 

Java




package com.example.gfgvalidator;
 
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.widget.CalendarView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
 
public class MainActivity extends AppCompatActivity {
 
    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        // Registering ID's
        TextView textView = findViewById(R.id.textView);
        CalendarView calenderView = findViewById(R.id.calendarView);
 
        // using setonDateChangeListener
        calenderView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(@NonNull CalendarView view, int year, int month, int dayOfMonth) {
                String date = dayOfMonth + "/" + month + "/" + year ;
 
                // changing the textview
                // data to selected date
                textView.setText(date);
            }
        });
 
        // for changing the background color of title bar
        ActionBar aBar = getSupportActionBar();
        ColorDrawable cd = new ColorDrawable(Color.parseColor("#FF00FF00"));
        if (aBar != null) {
            aBar.setBackgroundDrawable(cd);
        }
    }
 
}


Kotlin




package com.example.gfgvalidator;
 
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.widget.CalendarView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
 
class MainActivity : AppCompatActivity() {
   
    @RequiresApi(api = Build.VERSION_CODES.O)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
 
        // Registering ID's
        val textView = findViewById<TextView>(R.id.textView)
        val calenderView = findViewById<CalendarView>(R.id.calendarView)
 
        // using setonDateChangeListener
        calenderView.setOnDateChangeListener { view, year, month, dayOfMonth ->
            val date = "$dayOfMonth/$month/$year"
 
            // changing the textview
            // data to selected date
            textView.text = date
        }
 
        // for changing the background color of title bar
        val aBar = supportActionBar
        val cd = ColorDrawable(Color.parseColor("#FF00FF00"))
        aBar?.setBackgroundDrawable(cd)
    }
 
}


Output:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads