diff options
| author | Eric Fischer <enf@google.com> | 2009-07-23 18:32:42 -0700 |
|---|---|---|
| committer | Eric Fischer <enf@google.com> | 2009-07-23 18:48:44 -0700 |
| commit | 03a8017d0fe3b55b69c4328aa0d27bd96a2f1360 (patch) | |
| tree | 5557f7ca36e7736cea8e105b8d6e4e60719c6376 /core/java/android/widget/DatePicker.java | |
| parent | 1d4b87d492dfefb5506e9fcf358ced680322b754 (diff) | |
Make the DatePicker respect the date format setting if the date is numeric.
In some locales, there are no abbreviated month names; the abbreviated
date formats are essentially numeric. If the user is in such a locale,
have the DatePicker respect the date format setting so that the order
of the fields will match other numeric-only dates.
In locales that have abbreviated month names, continue to use the order
that is normal in spelled-out dates.
And update the order in updateDate() so that the new order is reflected
if you change the order setting and immediately go to change the date
without leaving and returning to the Date & Time settings in between.
At the same time, change DateFormat.getDateFormatOrder() back to working
the way it did in cupcake (prioritizing the date order preference over
the locale), even though the DatePicker no longer calls the method.
Bug 1805085
Diffstat (limited to 'core/java/android/widget/DatePicker.java')
| -rw-r--r-- | core/java/android/widget/DatePicker.java | 77 |
1 files changed, 60 insertions, 17 deletions
diff --git a/core/java/android/widget/DatePicker.java b/core/java/android/widget/DatePicker.java index 3b9f1de4c66c..5e76cc3a28af 100644 --- a/core/java/android/widget/DatePicker.java +++ b/core/java/android/widget/DatePicker.java @@ -31,6 +31,7 @@ import com.android.internal.widget.NumberPicker; import com.android.internal.widget.NumberPicker.OnChangedListener; import java.text.DateFormatSymbols; +import java.text.SimpleDateFormat; import java.util.Calendar; /** @@ -101,7 +102,8 @@ public class DatePicker extends FrameLayout { mMonthPicker = (NumberPicker) findViewById(R.id.month); mMonthPicker.setFormatter(NumberPicker.TWO_DIGIT_FORMATTER); DateFormatSymbols dfs = new DateFormatSymbols(); - mMonthPicker.setRange(1, 12, dfs.getShortMonths()); + String[] months = dfs.getShortMonths(); + mMonthPicker.setRange(1, 12, months); mMonthPicker.setSpeed(200); mMonthPicker.setOnChangeListener(new OnChangedListener() { public void onChanged(NumberPicker picker, int oldVal, int newVal) { @@ -146,7 +148,7 @@ public class DatePicker extends FrameLayout { init(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), null); // re-order the number pickers to match the current date format - reorderPickers(); + reorderPickers(months); if (!isEnabled()) { setEnabled(false); @@ -161,30 +163,70 @@ public class DatePicker extends FrameLayout { mYearPicker.setEnabled(enabled); } - private void reorderPickers() { - char[] order = DateFormat.getDateFormatOrder(mContext); - - /* Default order is month, date, year so if that's the order then - * do nothing. + private void reorderPickers(String[] months) { + java.text.DateFormat format; + String order; + + /* + * If the user is in a locale where the medium date format is + * still numeric (Japanese and Czech, for example), respect + * the date format order setting. Otherwise, use the order + * that the locale says is appropriate for a spelled-out date. */ - if ((order[0] == DateFormat.MONTH) && (order[1] == DateFormat.DATE)) { - return; + + if (months[0].startsWith("1")) { + format = DateFormat.getDateFormat(getContext()); + } else { + format = DateFormat.getMediumDateFormat(getContext()); } - + + if (format instanceof SimpleDateFormat) { + order = ((SimpleDateFormat) format).toPattern(); + } else { + // Shouldn't happen, but just in case. + order = new String(DateFormat.getDateFormatOrder(getContext())); + } + /* Remove the 3 pickers from their parent and then add them back in the * required order. */ LinearLayout parent = (LinearLayout) findViewById(R.id.parent); parent.removeAllViews(); - for (char c : order) { - if (c == DateFormat.DATE) { - parent.addView(mDayPicker); - } else if (c == DateFormat.MONTH) { - parent.addView(mMonthPicker); - } else { - parent.addView (mYearPicker); + + boolean quoted = false; + boolean didDay = false, didMonth = false, didYear = false; + + for (int i = 0; i < order.length(); i++) { + char c = order.charAt(i); + + if (c == '\'') { + quoted = !quoted; + } + + if (!quoted) { + if (c == DateFormat.DATE && !didDay) { + parent.addView(mDayPicker); + didDay = true; + } else if ((c == DateFormat.MONTH || c == 'L') && !didMonth) { + parent.addView(mMonthPicker); + didMonth = true; + } else if (c == DateFormat.YEAR && !didYear) { + parent.addView (mYearPicker); + didYear = true; + } } } + + // Shouldn't happen, but just in case. + if (!didMonth) { + parent.addView(mMonthPicker); + } + if (!didDay) { + parent.addView(mDayPicker); + } + if (!didYear) { + parent.addView(mYearPicker); + } } public void updateDate(int year, int monthOfYear, int dayOfMonth) { @@ -192,6 +234,7 @@ public class DatePicker extends FrameLayout { mMonth = monthOfYear; mDay = dayOfMonth; updateSpinners(); + reorderPickers(new DateFormatSymbols().getShortMonths()); } private static class SavedState extends BaseSavedState { |
