Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
484 views
in Technique[技术] by (71.8m points)

android - Date picker doesn't change its language with the rest of my app

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...