I have an old project that supports multi-languages. I want to upgrade support library and target platform, Before migrating to Androidx
everything works fine but now change language not work!
I use this code to change default locale of App
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);
}
And call this method on each activity by override attachBaseContext
like this:
@Override
protected void attachBaseContext(Context newBase)
{
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
String language = preferences.getString(SELECTED_LANGUAGE, "fa");
super.attachBaseContext(updateResources(newBase, language));
}
I try other method to get string and I noticed that getActivity().getBaseContext().getString
work and getActivity().getString
not work. Even the following code does not work and always show app_name
vlaue in default resource string.xml.
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"/>
I share a sample code in https://github.com/Freydoonk/LanguageTest
Also getActivity()..getResources().getIdentifier
not work and always return 0!
from Change Locale not work after migrate to Androidx
No comments:
Post a Comment