Disable the previous date in calendar view in Android Kotlin

Sudish Kumar
2 min readJun 4, 2021

Hi friends,

In this article, we will see how to disable the previous date and also advance fetchers which we can perform in the calendar view.

In many apps, you will have seen a calendar for selecting a date, Like for Birthday, Select date for an appointment with the doctor, we also add an event, and more...

It is easy to implement the disabling feature in the calendar view. when you start writing code will give you an error but after completing writing disabling codes it will be right.

Source codes link is bellowed you can take reference from it.

Disable the Previous Date in CalendarView

In .xml file(activity_main.xml)

<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">

<CalendarView
android:id="@+id/calendarView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

</RelativeLayout>

In Class file(MainActivity.kt)

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// finding calendar view by id
val calendarView = findViewById<CalendarView>(R.id.calendarView)
//desabling privious date calendarView.setMinDate(System.currentTimeMillis() - 1000);
}
}

These codes are working fine you can try it.

Source codes

https://gitlab.com/sudishkumar/calendar-android-app/-/blob/master/app/src/main/java/com/edugaon/calendarapp/MainActivity.kt

Advance things about Calendar View

You can also add calendar background, Like image, drawable shape, color, and more...

android:background="@drawable/ic_launcher_background"

How to show selected Date in Calendar View

It is easy to implement this type of operation. For doing this set setOnDateChangeListener{} on your calendar view id.

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val calendarView = findViewById<CalendarView>(R.id.calendarView)
calendarView.setMinDate(System.currentTimeMillis() - 1000);

// for showing selected date
calendarView.setOnDateChangeListener { view, year, month, dayOfMonth ->
val monthValue = month + 1
Toast.makeText(this, "$dayOfMonth-$monthValue-$year", Toast.LENGTH_LONG).show()
}
}
}

Thanks a lot for reading

The codes are here (https://gitlab.com/sudishkumar/calendar-android-app)

I hope this article is helpful. if it was helpful then please clap and follow.

--

--

Sudish Kumar

Teach lead, Flutter Developer, Android Developer, Node Js Developer