0

I have an app that has a button resposible for changing the language inside this app. The only thing that is immune to changes is DatePicker.

Code that change my app's language:

polishLanguage.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            restartActivityInLanguage("pl");
        }
        
    });

private void restartActivityInLanguage(String language) {
    Locale locale = new Locale(language);
    Configuration config = new Configuration();
    config.locale = locale;
    config.setLocale(locale);
    Resources resources = getResources();
    resources.updateConfiguration(config, resources.getDisplayMetrics());
    getActivity().recreate();
}

DatePicker code in xml:

<DatePicker
                    android:id="@+id/datePicker"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="-25dp"
                    android:layout_marginBottom="-25dp"
                    android:calendarViewShown="false"
                    android:datePickerMode="spinner" />

DatePicker code in java:

product_expirationDate_datepicker = new DatePicker(getActivity().getApplicationContext());

    Calendar today = Calendar.getInstance();
    long now = today.getTimeInMillis();
    product_expirationDate_datepicker.setMinDate(now);

    product_expirationDate_datepicker.init(today.get(Calendar.YEAR), today.get(Calendar.MONTH), today.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() {

        @Override
        public void onDateChanged(DatePicker datePicker, int year, int month, int dayOfMonth) {
            int selctedMonth = month + 1;
            expirationDateString = (product_expirationDate_datepicker.getYear() + "-" + selctedMonth + "-" + product_expirationDate_datepicker.getDayOfMonth());
            Log.e("onDateChanged", "expirationDateString: " + expirationDateString);

            HAS_DATE_BEEN_CHANGED = true;

        }
    });

There's no error in logcat, the languaege just doesn't change. I have already tried: Set language to French in android DatePickerDialog and https://gist.github.com/gilbertwat/4631571 but nothing works.

Andrea
  • 57
  • 1
  • 7

1 Answers1

0
new DatePicker(getActivity().getApplicationContext());

Use an Activity and not an Application context for your date picker.

Your language-changing code is only overriding the activity context resources. And an activity context is the right context to use for user interface widgets anyway.

laalto
  • 150,114
  • 66
  • 286
  • 303
  • I change my code to: `product_expirationDate_datepicker = new DatePicker(getActivity().getBaseContext());` and the language still doesn't change. Names of months stay in the same language version as my mobile's lanaguage version – Andrea Jan 11 '21 at 11:43
  • `getActivity()` is enough to get an `Activity` that is-a `Context` – laalto Jan 11 '21 at 12:07
  • `product_expirationDate_datepicker = new DatePicker(getActivity())` also doesn't work – Andrea Jan 11 '21 at 12:19