Android Date Range Picker


Photo Credit: Philip Chapman Bell - Android Date Range Picker Example

 

While developing a calender related Android app, selection of a date range is one of the most basic things one can think of. But sadly, with Android’s native date picker we cannot select a range of dates. Nor we can select multiple dates. Although to solve this, you can use two DatePickerDialogs, but a better solution would be to make use of a calender picker to select a date range. As you may know Android has a CalendarView widget to display calendars. But even this widget does not support multiple date selection or a date range selection. Therefore it is suggested to go for Android Times Square library by Square, Inc.

Android times square library mainly offers two great features on top of a normal calendar view. First it allows us to select a range of dates in Android. Second it allows us to select multiple dates, in no relation to each other(will get to this part later in the tutorial). There are many use cases where we have to select date ranges. First example that I can think of is an airline ticket booking app. Where you have to select a departure date and an arrival date. There are many such situations where this Android Times Square library can greatly reduce your development effort. Although if you still wish to use standard picker, you can refer to Date Picker Example.

 

Android Date Range Picker by TimesSquare Library

Once you have decided to use TimesSquare library, for selecting a date range in your android app. Implementing this library and using it, is not that difficult. First include its dependency in the gradle file:

 

Next include com.squareup.timessquare.CalendarPickerView tag, in the layout file:

Further initialize it, in the onCreate section of your activity:

 

This would give a great looking Android calendar view with an option to pick a range of dates, as shown below:

Android Date Range Picker

Also in the above example, I used a menu action button to finally select the dates. Here for the sake of simplicity I displayed them in a Toast message. But in your app, you can fire an Intent to launch another activity or return the results back to the calling activity.

 

Now lets understand how this CalendarPickerView widget is displayed. This widget can be customized to show only a range of dates for selection. For example if you want to restrict the user from selecting dates outside a range, you can do so from Android TimesSquare Library. As this library provides an easy init method which uses the start and end dates parameter to impose date restrictions in the calendar view. Although, don’t confuse this feature with the actual Android date range selection feature. As shown above, by the usage of .inMode(CalendarPickerView.SelectionMode.RANGE), the displayed CalendarPickerView will be selecting a range of dates.

 

Highlighting  Dates in an Android Date Picker

Another great feature of Android date range picker library by Square is, highlighting dates. Yes with TimesSquare you can highlight dates by adding the “to be highlighted” dates in a collection and specifying them through CalendarPickerView.highlightDates method. This feature can be used to specify the holidays in a calendar, as shown in the example above, or can be used to highlight some special days as per your requirement. I randomly chose 21/04/15 and highlighted it as holiday in the above date range picker calendar.

 

Picking Multiple Dates from the Calendar

As I said above this date range picker library is customizable and can be used to pick multiple dates as well. Android TimesSquare library has a selection mode parameter which can be used to specify this. To select multiple dates through this library just use .inMode(CalendarPickerView.SelectionMode.MULTIPLE)  method in the code above while initializing the CalendarPickerView by the init method. It will give you a calendar where you will be able to select multiple dates with no relation between them. As shown in the screenshot below:

Android Multiple Date Picker

 

Picking Single Date from Android TimesSquare Library CalendarPickerView

If you are amazed by the look and feel of this calendar view, and want to use it instead of a normal Android CalendarView widget, this can also be done. As this library also supports the basic functionality of simply selecting a single date from calendar. Just use the .inMode(CalendarPickerView.SelectionMode.SINGLE) method with specified parameter while initializing the CalendarPickerView widget. See the screen shot below:

Android Times Square Calendar View

 

Conclusion

I would like to conclude by saying that Android TimesSquare Library, is an easy to use library. Designed for usage in activities, fragments and dialogs. This library can primarily be used for selection of a date range in Android. As this is something important and not present in the standard Android calendar view. Also this CalendarPickerView provides the ability to select multiple dates and highlight important dates which is something new. Hope this helped, connect with us on Facebook, Google+, and Twitter to get more updates.

About Mohit Gupt

An android enthusiast, and an iPhone user with a keen interest in development of innovative applications.


Leave a comment

Your email address will not be published. Required fields are marked *

7 thoughts on “Android Date Range Picker