Different styles and strings are defined for the app. On Android prior to 8 everything is OK, but on Android 8 styles are not loaded correctly.
Note: strings are fine. Its just styles.
res
values
styles.xml
strings.xml
values-fr
styles.xml
strings.xml
values-v21
styles.xml
I changed the app locale like this:
public static Context changeAppLocale(String lang, Context c) {
Locale locale = new Locale(lang);
Resources resources = c.getResources();
Configuration config = new Configuration(resources.getConfiguration());
Locale.setDefault(locale);
if(Build.VERSION.SDK_INT > 16) {
config.setLayoutDirection(locale);
}
if(Build.VERSION.SDK_INT > 24){
LocaleList localeList = new LocaleList(locale);
LocaleList.setDefault(localeList);
config.setLocale(locale);
config.setLocales(localeList);
c = c.createConfigurationContext(config);
}
else{
config.locale = locale;
resources.updateConfiguration(config, resources.getDisplayMetrics());
}
return c;
}
And I apply it inside the activity like this:
protected void attachBaseContext(Context newBase) {
context = G.changeAppLocale(G.appLang, newBase);
super.attachBaseContext(context);
}
Once again, strings are loaded correctly, but styles not (Just on Android 8). Are different styles not supported on Android anymore?
I'm answering my own question in case somebody has the same problem and it may help them.
I was inflating the layout dynamically and I was using a static inflater that was instantiated by an old context.
// in class extended from Application
public static LayoutInflater globalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
And when I was inflating:
...
pageView = (ViewGroup) G.globalInflater.inflate(R.layout.layout_main_log, container, false);
...
But that inflater was not updated when I was changing the app locale. Thus views would be inflated from an old locale. Now I instantiate the inflater every time I want to inflate a view:
...
Context context = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(inflater != null){
pageView = (ViewGroup) inflater.inflate(R.layout.layout_main_log, container, false);
}
...
Thank you all for your time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With