9

I have been trying to get an answer to this question for quite a while. I also had a look at the following link: Android - DatePicker Widget Format. On my phone/ emulator (v 2.3.3) both the DatePicker widget and the dialog do not take the date format specified in settings (Settings->Date&Time->Select date format). They are always in the format "MM/dd/yyyy". The locale is set to English UK.

I get "d/MM/yyyy" when I read the settings with:

Settings.System.getString(getContentResolver(), Settings.System.DATE_FORMAT);

Does anybody have an idea about how to change the date format in a DatePicker view?

Community
  • 1
  • 1
Ovid
  • 113
  • 1
  • 1
  • 6

2 Answers2

13

To override the DatePicker format the best bet would be to override one of its constructors with a slight edit to the original code. You can edit the ViewGroup in code for you but I'd seriously consider not doing it that way.

  1. Create class LocalizedDatePicker
  2. Override public DatePicker (Context context, AttributeSet attrs, int defStyle) http://developer.android.com/reference/android/widget/DatePicker.html#DatePicker(android.content.Context, android.util.AttributeSet, int)
  3. Copy the code from the original DatePicker constructor (you may need to select a different branch after some testing, I have linked the head)
  4. Override methods that call reorderSpinners() to remove that call.
  5. Replace line inflater.inflate(R.layout.date_picker, this, true); with inflater.inflate(my.package.R.layout.localized_date_picker, this, true);
  6. Copy the original date_picker.xml layout to your local resource localized_date_picker.xml
  7. Edit the file for the desired order, since you're editing this please respect the localization of other places and keep a copy of the original in your global layout folder and put the localized_date_picker.xml in your region specific folder. Since the system's layout is Month Day Year people in other places may expect that order.
Dan S
  • 9,139
  • 3
  • 37
  • 48
  • Thanks a lot for the comprehensive answer. It is a shame that so much needs to be done for what appears to be a bug in the En UK locale (see [Date and time notation in the United Kingdom](http://en.wikipedia.org/wiki/Date_and_time_notation_in_the_United_Kingdom)). I suppose that at no 6 you mean reorderPickers() instead of reorderDates()? – Ovid Aug 28 '11 at 20:56
  • original DatePicker constructor link is broken – Ravi K. Sharma Jun 09 '14 at 12:07
  • @DanS Can you elaborate please .where is reorderPickers() ? – IntelliJ Amiya Jul 29 '15 at 11:10
  • 1
    @IntelliJAmiya The method was renamed to reorderSpinners() – Dan S Jul 29 '15 at 22:28
3

That setting is very specific. It is only meant to apply to dates dates shown in "short format". As long as you're using anything else (in this case, the DatePicker seems to be using "medium"), then those settings will be disregarded and the Locale's default formatting will take over.

I can confirm that if you change your phone's the language (and, effectively, the Locale) the order of the elements does changes in the date picker. Which I guess means I disagree with Dan S's assertion that "the system does not use any mechanism to localize the DatePicker". If you look at the code, you can see that (at least since 2.1), there's a reorderPickers() method that does exactly this.

English: English

French: French

dmon
  • 30,048
  • 8
  • 87
  • 96
  • 2
    Thanks for the answer, I do understand now how this works. It is funny that the date_picker.xml seems to contradict this behaviour: _Warning: everything within the parent is removed and re-ordered depending on the date format selected by the user._ Anyway, since the current locale seems to be used, that means to me that the default date format in En UK locale (mm/dd/yyyy) is wrong, as it should be dd/mm/yyyy: [Date and time notation in the United Kingdom](http://en.wikipedia.org/wiki/Date_and_time_notation_in_the_United_Kingdom). – Ovid Aug 28 '11 at 21:17
  • @Ovid English in context of America..and there are many wrong implementations in the Android framework related to localisation.. – Tobrun Jul 10 '14 at 13:01