i'm using below code to change my app locale. this works fine in android O and later but not working in android N and older versions. where is problem?
public class CustomContextWrapper extends ContextWrapper {
private CustomContextWrapper(Context base) {
super(base);
}
public static CustomContextWrapper wrap(Context context, String lang) {
Locale locale;
if (lang.length() > 2) {
String[] langWithRegion = lang.split("-");
locale = new Locale(langWithRegion[0], langWithRegion[1]);
} else
locale = new Locale(lang);
Locale.setDefault(locale);
Configuration configuration = context.getResources().getConfiguration();
configuration.setLocale(locale);
configuration.setLayoutDirection(locale);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
LocaleList localeList = new LocaleList(locale);
LocaleList.setDefault(localeList);
configuration.setLocales(localeList);
}
return new CustomContextWrapper(context.createConfigurationContext(configuration));
}
}
my Application class
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(CustomContextWrapper.wrap(base,"fa"));
}
and my BaseActivity
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(CustomContextWrapper.wrap(base,"fa"));
}