everyone! I have been making a small application with my friend, and we have a problem. My task in this 'making' is changing the application's language by choosing the language in the spinner and pressing the 'save' button, which must change the entire application's language, no matter what the phone's language is. However, I can't make this happen, and despite I've tried things for a lot of times, nothing worked, as it only changed the MainActivity's language. The code I've used is at below:
public class LocaleHelper{
private static final String SELECTED_LANGUAGE = "Locale.Helper.Selected.Language";
public static Context onAttach(Context context) {
String lang = getPersistedData(context, Locale.getDefault().getLanguage());
return setLocale(context, lang);
}
public static Context onAttach(Context context, String defaultLanguage) {
String lang = getPersistedData(context, defaultLanguage);
return setLocale(context, lang);
}
public static String getLanguage(Context context) {
return getPersistedData(context, Locale.getDefault().getLanguage());
}
public static Context setLocale(Context context, String language) {
persist(context, language);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return updateResources(context, language);
}
return updateResourcesLegacy(context, language);
}
private static String getPersistedData(Context context, String defaultLanguage) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getString(SELECTED_LANGUAGE, defaultLanguage);
}
private static void persist(Context context, String language) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(SELECTED_LANGUAGE, language);
editor.apply();
}
@TargetApi(Build.VERSION_CODES.N)
private static Context updateResources(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
Configuration configuration = context.getResources().getConfiguration();
configuration.setLocale(locale);
return context.createConfigurationContext(configuration);
}
@SuppressWarnings("deprecation")
private static Context updateResourcesLegacy(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
configuration.locale = locale;
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
return context;
}
}
I'ved used the function setLocale() to change the language, and the code for the change is at below:
@Override
public void onPositiveClicked(String language) {
//Change Application level locale
LocaleHelper.setLocale(getApplicationContext(), language);
//It is required to recreate the activity to reflect the change in UI.
recreate();
}
And this is what happens if we press the save button:
case R.id.savebutton:
if(languageSpinner.getSelectedItem().toString().equals(languageArray[0])) {
String languageString = "ko";
customDialogListener.onPositiveClicked(languageString);
}
else if (languageSpinner.getSelectedItem().toString().equals(languageArray[1])) {
String languageString = "en";
customDialogListener.onPositiveClicked(languageString);
}
dismiss();
break;
case R.id.closebutton:
cancel();
break;
I think there is a problem in LocaleHelper or the function 'recreate()', so if there is anyone who can lead me to the answer, I will really, really appreciate it. Ah, and I've written the code in java so please answer in java, please!