Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to change the DatePicker view date format from MM/dd/yyyy to dd/MM/yyyy?

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?

like image 589
Ovid Avatar asked Aug 26 '11 18:08

Ovid


People also ask

How can I change DatePicker size in android?

For example, android:layout_width="15dp" will set the width at a scalable value of 15. You can change 15 to whatever you want. If you want to set a specific pixel size, you can use, for example, 15px .


1 Answers

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.
like image 128
Dan S Avatar answered Sep 17 '22 07:09

Dan S