Changing Locale At Runtime Not Effecting Fragments
Solution 1:
I assume that you are using this approach. Where its not suggested any change in the AndroidManifest.xml
so the android:configChanges="locale"
may cause the misbehavior you defined.
For the date formating you should take into consideration that your application is not using the Locale.getDefault()
but something different that is defined by the user and your LocaleHelper
mechanism.
Some extra details about config change
The android:configChanges
means you don't want the system to recreate your activity when one of the provided attribute happens. In your case the locale
.
In terms of development with that approach in order to properly handle this option in the manifest you have to implement the
@OverridepublicvoidonConfigurationChanged(Configuration newConfig) {
// refresh your views heresuper.onConfigurationChanged(newConfig);
}
and then perform your own handling. Something that you didn't required in your case.
Solution 2:
Application class which extended in the manifest file.
package com.example.languageapplication;
import android.content.Context;
publicclassApplicationextendsandroid.app.Application {
privatestatic Application applicationInstance;
publicstaticsynchronized Application getInstance() {
return applicationInstance;
}
@OverridepublicvoidonCreate() {
super.onCreate();
applicationInstance = this;
}
publicvoidinitAppLanguage(Context context) {
LocaleUtils.initialize(context, LocaleUtils.getSelectedLanguageId());
}
}
Locale utils class for set language in shared preferences and update configuration for set language
publicclassLocaleUtils {
publicstaticfinalStringENGLISH="en";
publicstaticfinalStringFRENCH="fr";
publicstaticfinalStringSPANISH="es";
publicstaticvoidinitialize(Context context, @LocaleDef String defaultLanguage) {
setLocale(context, defaultLanguage);
}
publicstaticbooleansetLocale(Context context, @LocaleDef String language) {
return updateResources(context, language);
}
privatestaticbooleanupdateResources(Context context, String language) {
Localelocale=newLocale(language);
Locale.setDefault(locale);
Resourcesresources= context.getResources();
Configurationconfiguration= resources.getConfiguration();
context.createConfigurationContext(configuration);
configuration.locale = locale;
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
returntrue;
}
@Retention(RetentionPolicy.SOURCE)@StringDef({ENGLISH, FRENCH, SPANISH})public@interface LocaleDef {
String[] SUPPORTED_LOCALES = {ENGLISH, FRENCH, SPANISH};
}
privatestatic SharedPreferences getDefaultSharedPreference(Context context) {
if (PreferenceManager.getDefaultSharedPreferences(Application.getInstance().getApplicationContext()) != null)
return PreferenceManager.getDefaultSharedPreferences(Application.getInstance().getApplicationContext());
elsereturnnull;
}
publicstaticvoidsetSelectedLanguageId(String id){
finalSharedPreferencesprefs= getDefaultSharedPreference(Application.getInstance().getApplicationContext());
SharedPreferences.Editoreditor= prefs.edit();
editor.putString("app_language_id", id);
editor.apply();
}
publicstatic String getSelectedLanguageId(){
return getDefaultSharedPreference(Application.getInstance().getApplicationContext())
.getString("app_language_id", "en");
}
}
set Language on click of a button in another activity and restart launcher activity
publicclasssetLaunguageActivityextendsAppCompatActivity {
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
publicvoidsetEnglish(View view) {
LocaleUtils.setSelectedLanguageId("en");
Intenti= getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());
startActivity(i);
}
publicvoidsetFrance(View view) {
LocaleUtils.setSelectedLanguageId("fr");
Intenti= getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());
startActivity(i);
}
}
Initialize Application class in the main activity like this
publicclassMainActivityextendsAppCompatActivity {
TextView textInfo;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Application.getInstance().initAppLanguage(this);
setContentView(R.layout.activity_main);
}
this code is working for me
Post a Comment for "Changing Locale At Runtime Not Effecting Fragments"