From 9de147b69e232d167bc18805735ae085e434f31b Mon Sep 17 00:00:00 2001 From: Rohit Yengisetty Date: Thu, 8 Jan 2015 12:29:15 -0800 Subject: Datetimepicker : Account for RTL locales when formulating Day labels Doing a simple substring for the first character to generate a single character day label doesn't work for right-to-left(RTL) and chinese languages. Change-Id: Ic7d2a7880179326530830d8deee0f668dde335c8 --- src/com/android/datetimepicker/Utils.java | 17 +++++++++++++ src/com/android/datetimepicker/date/MonthView.java | 28 +++++++++++++++++++--- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/com/android/datetimepicker/Utils.java b/src/com/android/datetimepicker/Utils.java index 4a3110c..0b57f7c 100644 --- a/src/com/android/datetimepicker/Utils.java +++ b/src/com/android/datetimepicker/Utils.java @@ -25,6 +25,7 @@ import android.text.format.Time; import android.view.View; import java.util.Calendar; +import java.util.Locale; /** * Utility helper functions for time and date pickers. @@ -137,4 +138,20 @@ public class Utils { return pulseAnimator; } + + /** + * Determines if the device's current default locale uses right-to-left written script + */ + public static boolean isRtl() { + return isRtl(Locale.getDefault()); + } + + /** + * Determines if a particular locale uses right-to-left written script + */ + public static boolean isRtl(Locale locale) { + final int directionality = Character.getDirectionality(locale.getDisplayName().charAt(0)); + return directionality == Character.DIRECTIONALITY_RIGHT_TO_LEFT || + directionality == Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC; + } } diff --git a/src/com/android/datetimepicker/date/MonthView.java b/src/com/android/datetimepicker/date/MonthView.java index a0f7671..43978b8 100644 --- a/src/com/android/datetimepicker/date/MonthView.java +++ b/src/com/android/datetimepicker/date/MonthView.java @@ -552,14 +552,17 @@ public abstract class MonthView extends View { int y = getMonthHeaderSize() - (mMonthDayLabelTextSize / 2); int dayWidthHalf = (mWidth - mEdgePadding * 2) / (mNumDays * 2); + boolean isRtl = Utils.isRtl(); // current locale's written script direction for (int i = 0; i < mNumDays; i++) { int calendarDay = (i + mWeekStart) % mNumDays; int x = (2 * i + 1) * dayWidthHalf + mEdgePadding; mDayLabelCalendar.set(Calendar.DAY_OF_WEEK, calendarDay); - String label = mDayLabelCalendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, - Locale.getDefault()).toUpperCase(Locale.getDefault()); - canvas.drawText(label.substring(0, 1), x, y, mMonthDayLabelPaint); + String dayDisplayName = mDayLabelCalendar.getDisplayName(Calendar.DAY_OF_WEEK, + Calendar.SHORT, Locale.getDefault()).toUpperCase(Locale.getDefault()); + + String dayLabel = generateDayLabel(dayDisplayName, isRtl); + canvas.drawText(dayLabel, x, y, mMonthDayLabelPaint); } } @@ -905,4 +908,23 @@ public abstract class MonthView extends View { public interface OnDayClickListener { public void onDayClick(MonthView view, CalendarDay day); } + + /** + * Formats a given Day's display name into a single character label + * + * @param input display name of the day (Saturday, Monday .. etc) + * @param isRtl true if the input's written script is right-to-left + * @return + */ + private String generateDayLabel(String input, boolean isRtl) { + Locale locale = Locale.getDefault(); + // 'special-case'-ing Chinese languages as their Day labels are written right-to-left + if (isRtl || locale.equals(Locale.SIMPLIFIED_CHINESE) || + locale.equals(Locale.TRADITIONAL_CHINESE) || + locale.equals(new Locale("zh", "HK"))) { + return input.substring(input.length()-1); + } else { + return input.substring(0,1); + } + } } -- cgit v1.2.3