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
1.0k views
in Technique[技术] by (71.8m points)

android - The app doesn't get the localization change effect on nougat api 7 ++

I have a custom adapter for my spinner:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item) {

      .............

    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    adapter.add(context.getResources().getString(R.string.value1));
    adapter.add(context.getResources().getString(R.string.value2));
    adapter.add(context.getResources().getString(R.string.hint));
    spinner.setAdapter(adapter);

Every thing work as expect but list adapter, when i change the application language every thing got the language change effect but the list adapter don't.

I have the resource for both language.

I'm changing language via this method:

public  void setLocale(String lang) {
    Locale myLocale = new Locale(lang);
    Resources res = getResources();
    DisplayMetrics dm = res.getDisplayMetrics();
    Configuration conf = res.getConfiguration();
    conf.locale = myLocale;
    res.updateConfiguration(conf, dm);

}

After i test the case on lower device like lolipop it's worked as well, the current issue with android oreo 8.0.

Since conf.locale = myLocale; was deprecated in API level 24.

So i didn't use conf.locale = myLocale;directly. Use getLocales() and setLocales(LocaleList). If only the primary locale is needed, getLocales().get(0) is now the preferred accessor.

Also updateConfiguration was deprecated in API level 25, I used createConfigurationContext (Configuration overrideConfiguration) instead.

But it didn't work, Is i missing something?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

This issue fixed after passing the Activity context instead of applicationContext to the spinnerAdapter.

Simply I changed spinnerAdapter = new SpinnerAdapter(getApplicationContext()); to spinnerAdapter = new SpinnerAdapter(MainActivity.this);

A REF.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
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

...