(EDIT)
I used a solution for locale change to change the language of my app and it is not working in oreo. It's working perfectly on my samsung S4, but not on my S9.
So I am doing the locale change like this:
public void initAppLanguages(Context context, String lang){
PreferenceUtil.setSelectedLanguageId(lang);
LocaleUtils.setLocale(context, lang );
MyApplication.reouvrir=1;
Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());
this.finishAffinity();
finish();
startActivity(i);
}
My LocaleUtils class:
public class LocaleUtils {
@Retention(RetentionPolicy.SOURCE)
@StringDef({ENGLISH, FRENCH, SPANISH})
public @interface LocaleDef {
String[] SUPPORTED_LOCALES = {ENGLISH, FRENCH, SPANISH};
}
public static final String ENGLISH = "en";
public static final String FRENCH = "fr";
public static final String SPANISH = "es";
public static void initialize(Context context) {
setLocale(context, ENGLISH);
}
public static void initialize(Context context, @LocaleDef String defaultLanguage) {
setLocale(context, defaultLanguage);
}
public static boolean setLocale(Context context, @LocaleDef String language) {
return updateResources(context, language);
}
private static boolean updateResources(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
context.createConfigurationContext(configuration);
configuration.locale = locale;
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
return true;
}
}
My PreferenceUtil class:
public class PreferenceUtil {
private static SharedPreferences getDefaultSharedPreference(Context context) {
if (PreferenceManager.getDefaultSharedPreferences(MyApplication.getInstance().getApplicationContext()) != null)
return PreferenceManager.getDefaultSharedPreferences(MyApplication.getInstance().getApplicationContext());
else
return null;
}
public static void setSelectedLanguageId(String id){
final SharedPreferences prefs = getDefaultSharedPreference(MyApplication.getInstance().getApplicationContext());
SharedPreferences.Editor editor = prefs.edit();
editor.putString("app_language_id", id);
editor.apply();
}
public static String getSelectedLanguageId(){
return getDefaultSharedPreference(MyApplication.getInstance().getApplicationContext())
.getString("app_language_id", "en");
}
}
And the locale change in the child activities like this:
MyApplication.initAppLanguage(mContext);
What am I doing wrong? Why is it not working in Oreo?
Start your activity after set the new locale. That worked for me.
You can see the full answer here
Follow the steps below:
Note: I assume that you created different string file in values folder for different english, spanish and french. (i.e strings.xml file in each values-en, values-es, values-fr folder in res directory)
Add this file: LocaleHelper.java
Add file: LanguageUtil.java
Add file: MyContextWrapper.java
Add file: VersionUtils.java
Now Create Prefs file: Prefs.kt (This is Kotlin class file) Note: You can use your own preference creation and retrivation code.
package com.test
Now create one class TestApp which extends the Application class. This will use to save the last selected language you want to apply when app restarts. Note: don't forget to add this app class name "TestApp" to AndroidManifest.xml in tag.
Add file: TestApp.kt
Now in each activity, add the code below to apply the changed language.
This is Kotlin code:
When you select the different language, use this code:
and after also store it in sharedpreference.
Remember: If you change language in current Activity and you will not see the language change in that, because you need to restart activity to see the changes but activity opened after this current activity you will see the changes.
If you call recreate() in the same activity, it will blink the screen, so do not use the recreate() function.
I have one solution (not proper solution but I can say it is patch and it is worked perfect). For displaying changes in current activity you need to create all the english, spanish and french string in default strings.xml and set it runtime in current activity when the user selects a different language.