Android Material Datepicker and Timepicker by Wdullaer Tutorial by Example

Android is already packed with great Calender and Timepicker to add-in application. Which most of the time fulfill basic requirements needs of a project. But some times few challenges come on ways which call for more flexibilities in Calander date pickers.

A few days back I was working on a project with Booking appointment module. There was a requirement to disable all Sundays and Saturdays in calendar datepicker dialog so that nobody will be able to select disabled dates. But there is no possibility to achieve that in traditional datepicker Calender.

After some research, I came across a good substitute for Date and Time pickers in Android application. Android Material Date and Time picker is already a choice of many developers and its creator Wdullaer really quick on updates and fixes.

Here we will get to know how to implement Android Material Date and Timepicker in the application and also discuss some of the major methods available and will also explore its important options.

Let’s start with a new Android project, here my Android Studio’s current version is v3.4.2

Create a new project with the “Empty” template.

Open Module app’s build.gradle file then add following in dependencies

After that click on “Sync Now” to make changes and download package.

Next, we will add Two Buttons “Open Datepicker” and “Open Timepicker” and Two text fields to show selected values.

Now open “~activity_main.xml” file then replace following XML layout code in it.

 

In MainActivity.java file we will call Datepicker and Timepicker methods after adding click listeners on Buttons we added in our layout file.

Import packages required in the file

 

also, implement interfaces to add callbacks for After selection of values from the date and time picker.

Define some variables to keep date and time and also Datetime dialog instances

In the onCreate method, we will add click listeners will call Datepicker and Timepicker dialogs

Datepicker

 

Timepicker

Now let’s add callback methods onDateSet for date and onTimeSet time to get selected values

Set Min Date and Max Date in Datepicker

To set Min and Max date validations we can use setMinDate() and setMaxDate() methods as follows

Disable specific dates in Datepicker

There are three very useful methods available which take Calander array of days.

setSelectableDays(Calendar[] days): The values in this list are the only acceptable dates for the picker.
setDisabledDays(Calendar[] days): The values in this Calendar[] are explicitly disabled (not selectable).
setHighlightedDays(Calendar[] days): Pass a Calendar[] of days to highlight.

Here is an example of Disable All SUNDAYS and SATURDAYS in Datepicker which are lying in between the Min and Max dates.

Our complete MainActivity will have the following code

 

Conclusion: Android Material Date and Time picker are loaded with many configuration options. It is very easy to use and implement in a new or existing project. You can surely use this a substitute to Android’s default Calendar view.

Let me know if you are stuck to try something we can look and explore more into it.

Thanks for visiting :) Let me know if you have some suggestions or feedback for this post in the comment section. Looking for any tutorial or demo on FreakyJolly, just drop me a message here

Leave a Reply

avatar
  Subscribe  
Notify of