Categories
Android

How to use Android’s CalendarView widget

tl;dr: DON’T!!!

Really – stay away from it.

How it should be

The CalendarView is a regular widget in Android that was added in API level 11 (that is Android 3.0). It can be used to view and select dates from a calendar.

This seemed like a good widget for my application for picking dates so I started to use it. Unfortunately (or luckily?) in the end of the day I have removed all references to CalendarView from the code.

What it really is

Here’s my findings from the time that I played with the widget before I got tired of finding a bug after another and threw it out of the window.

1. Doesn’t follow the default locale

My application is and will be only in Finnish so I have set the language for the application to Finnish with Locale.setDefault(). I have done this so that UI elements like date pickers would be in Finnish (like the rest of the UI) even if the system language is set to something else by the user.

It turned out that the CalendarView doesn’t care about the default locale on Android 4.1.2 at least. It seems to respect it in 4.2.2 though, so it’s been fixed somewhere there in between. But 4.2.2 is a really recent version of Android and a large majority is still using something older.

2. …and even if it does it does it wrong

incorrect-finnish-calendar

So on Android 4.2.2 I get the Finnish texts for the widget if I have the default locale set – except they are wrong. This bug might not be obvious to non-Finnish speakers since e.g. English language doesn’t have the same kind of feature that causes this.

Check this image. The month says “heinäkuuta” which is July in Finnish. The problem is that in a situation like this where there is no date attached to the month name it should be in a “stand alone” form. The correct Finnish form should read “heinäkuu”.

Another issue is that although the texts are now in Finnish the week still starts from Sunday which is not the case in Finland. We start the week from Monday.

3. setMinDate() does what?

year-1964

The setMinDate() method is used to set the minimum date that the calendar should view and the user should be able to select. It does that – but it does more. I guess it sets the current date to the maximum date (which is similar to the minimum date but in the other end) which is somewhere on the year 2100 by default.

This is bad by itself. But in addition the widget doesn’t show dates after around year 2038 correctly and renders the year 2100 as 1964. See the image and note how the next month isn’t shown after the 30th day since that is the (default) maximum date viewed.

There is a bug report and a stackoverflow page about this where there is more information and analysis for those who want to dig in on the subject.

I give up

At this point I ditched the CalendarView widget and went to search for other options. Just for amusement here is a screen shot from a real situation that you can have with the DatePicker widget which uses the CalendarView too. Those two pickers should synchronize the current date but that doesn’t work. The spinners on the left show 30th of July 2013 while the calendar view on the right shows – actually nothing. There is no date selected (which should be visible similarly to the first image on this article). It shows a month in the year 2100 and renders it incorrectly. Well done, mate.

year-2013-1964

4 replies on “How to use Android’s CalendarView widget”

What a useless post, regardless of your problems, you should have at least shwon how you attempted to implement the calendarView so others could try to better it or work around the problems. You’ve literally just whinged about a view.

Thanx for the help. I wasted my whole time to know how would I select an year in calendar view. Thanks for the date picker fragment

Leave a Reply

Your email address will not be published.