Set default Locale to DatePicker and TimePicker
Asked Answered
P

3

12

I'm using a DatePicker and a TimePicker in a custom Dialog. The functionality works fine, but I have a problem when I change the default Locale of the app to match the Arabic language.

The problem is that the DatePicker and TimePicker are not localized on all Android versions. It works on Android versions 4.2.2 and higher. But It doesn't work on Android versions 4.1.2 And lower.

I want to either change localization of DatePicker and TimePicker to English all the time, Or to change it to Arabic when the Locale matches the Arabic language.

This is the dialog in Android versions 4.2.2 and higher (the correct one that matches the localization)

Android version 4.2.2 and higher

And this is the dialog in Android versions 4.1.2 and lower

Android versions 4.1.2 and lower

Paramnesia answered 21/10, 2014 at 13:18 Comment(0)
K
3

You need to create your custom DatePicker or TimePicker . See this link https://mcmap.net/q/1012827/-android-datepicker-typeface. It can help you to solve your problem.

Or you can use Reflection to edit the NumberPicker values, follow this link for the complete solution.

Kolomna answered 21/10, 2014 at 14:32 Comment(3)
Is there a way to change the Months strings in the DatePicker instead of creating a custom one?Paramnesia
I see that you found some solution. In other case unfortunately i don't see any possibility to do it. Here link to customize your DatePicker https://mcmap.net/q/597011/-set-language-to-french-in-android-datepickerdialogKolomna
@AbdallahAlaraby any solution for this without custom?Receipt
L
0

if you pass the current locale to SimpleDateFormat Object you will get your locale picker for example you can consider following snippet code

@NotNull
public static Calendar getPickedTime(@NotNull String selectedTimeStr) {
    Calendar calendar=Calendar.getInstance();
    SimpleDateFormat simpleDateFormat=new SimpleDateFormat("hh:mm aa",Locale.US);
    try {
        calendar.setTime(simpleDateFormat.parse(selectedTimeStr));
        Log.d(TAG, "getPickedTime: "+calendar.getTime());
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return calendar;
}
Lamellate answered 22/10, 2022 at 19:8 Comment(0)
A
-2

Please change the locale in your fragment/activity, before showing DatePicker/TimePicker.

Locale locale = Locale.US; //Your specific locale. Locale.setDefault(locale);

    Configuration configuration = context.getResources().getConfiguration();
    configuration.setLocale(locale);
    configuration.setLayoutDirection(locale);

    context.createConfigurationContext(configuration);
Annmaria answered 12/3, 2019 at 9:21 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.