Creating a Calendar View app in Android
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.
onSelectedDayChange: In this method, we get the values of days, months, and years that are selected by the user.
Step by Step Implementation
Step 1: Create a new project in Android Studio
Below are the steps to be followed:
- Click on File, then New => New Project.
- After that include the Kotlin support and click on next.
- Select the minimum SDK as per convenience and click next button.
- Then select the Empty activity => next => finish.
Step 2: Working with activity_main.xml
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.
activity_main.xml:
<androidx.constraintlayout.widget.ConstraintLayout
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"
android:background="@color/white"
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:text="Set the Date"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/calendar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!-- Add CalendarView to display the Calendar -->
<CalendarView
android:id="@+id/calendar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Design UI:
Step 3: Working with MainActivity.kt
Open up the MainActivity file and define the CalendarView and TextView type variable, and also use findViewById() to get both the views. 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.
MainActivity File:
package org.geeksforgeeks.demo;
import android.os.Bundle;
import android.widget.CalendarView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CalendarView calendar = findViewById(R.id.calendar);
TextView dateView = findViewById(R.id.date_view);
calendar.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
@Override
public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
String date = dayOfMonth + "-" + (month + 1) + "-" + year;
dateView.setText(date);
}
});
}
}
package org.geeksforgeeks.demo
import android.os.Bundle
import android.widget.CalendarView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val calendar: CalendarView = findViewById(R.id.calendar)
val dateView: TextView = findViewById(R.id.date_view)
calendar.setOnDateChangeListener { _, year, month, dayOfMonth ->
val date = (dayOfMonth.toString() + "-" + (month + 1) + "-" + year)
dateView.text = date
}
}
}