I am developing an app which supports two languages. There is one ToggleButton to change language.I am using following code to change locale and all works fine.
Resources res = viewDashBoardScreen.getResources(
// Change locale settings in the app.
DisplayMetrics dm = res.getDisplayMetrics();
android.content.res.Configuration conf = res.getConfiguration();
conf.locale = new Locale("es".toLowerCase());
res.updateConfiguration(conf, dm);
Question: How to refresh whole app after language change ?
Currently I am restarting that activity in which language is changed. It works fine but is it good practice ? Or is there any other way to apply language change in app.
Any help would be greatly appreciated..Thanks.
i thing you can call this function whn togal button change it's working for me
private void setLocale(String localeCode){
AppLog.logString(TAG+"set location function: "+localeCode);
locale = new Locale(localeCode);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
getApplicationContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
UserDetail.this.getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
onCreate(null);
}
on toggel buttong change call function this way :
setLocale("en-us");// pass languen in whicy u want to translate
setLocale("ar",savedInstanceStat);
Related
My Android Application is Multilanguage. When the user changing app Language, the app saves it in SharedPreferences to make changes permanent (set locale when the app opens).
The way I change language:
public static void changeInterfaceLanguage(Activity activity, #Language String lang) {
Resources res = activity.getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
Locale currentLocale = getCurrentLocale(activity);
final Locale newLocale = new Locale(lang);
if (!currentLocale.equals(newLocale)) {
SettingsPrefsEdit.putString(activity, SettingCons.INTERFACE_LANGUAGE, lang);
conf.setLocale(newLocale);
res.updateConfiguration(conf, dm);
activity.recreate();
}
The way I restore Locale when the app opens:
#Override protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String lang = SettingsPrefsGet.getString(activity, SettingCons.INTERFACE_LANGUAGE,
SettingCons.LANG_ENGLISH);
Locale locale = new Locale(lang);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
activity.getResources()
.updateConfiguration(config, activity.getResources().getDisplayMetrics());
...
}
Also, Admob Native Ads is implemented in this App.
The problem is this:
After destroying app and reopen it, exactly when my code calls any method from Admob library, the app language (Locale) is changing to English, and after that, every new screen of the app is in English.
Some of the methods that cause this problem:
MobileAds.initialize(this)
AdLoader.Builder adLoader = ...
adLoader.build().loadAd(new AdRequest.Builder().build());
Useful notes:
SharedPreferences doesn't get change when this problem occurs.
There is only one Activity in the app. All pages are implemented as Fragment (50+ pages)
After changing app language, everything is fine until app getting destroyed
I'm using Firebase version of Admob:
implementation 'com.google.firebase:firebase-ads:19.6.0'
What I've tried:
When I disable all calls to Admob methods, the problem disappears.
I moved locale update code block to MainApplication. But the problem exists yet.
I re-implemented Language change operation with this library, but didn't resolve:
https://github.com/YarikSOffice/lingver
I know how to use locale and strings.xml for global locale, in addition I created 3 buttons on rhe main activity with 3 different languages on them, how can I switch the whole strings in all screens in one click, how can I do that?
Step1:
Use a preference to store the locale language and country, if you want this language switching is persistent.
Step2:
Try a function like this
public void switchLocale(Locale locale) {
Configuration config = getBaseContext().getResources().getConfiguration();
if (locale != null && !config.locale.equals(locale)) {
Locale.setDefault(locale);
Configuration conf = new Configuration(config);
conf.locale = locale;
getBaseContext().getResources().updateConfiguration(conf,
getBaseContext().getResources().getDisplayMetrics());
}
}
Step3:
Restart your current activity.
Hope this helps.
When user changes the system locale from US to China. I don't want to change the language from English to Chinese in my app, I expect that my app's language still in English.
May I do this by setting a android attribute in AndroidManifest.xml?
Thanks
I think it would be better to do this once and put your code in your application class
public class MyApplication extends Application {
#Override
protected void attachBaseContext(Context base) {
Resources res = base.getResources();
Locale locale = new Locale("en");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
res.updateConfiguration(config, res.getDisplayMetrics());
super.attachBaseContext(base);
}
}
If you just want the Chinese xml to stay in the app but not plan in using it, you can simply comment all its content, and the App will consider only the English strings
You can use below code to maintain locale as per your requirement,
Locale locale = new Locale("en");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
When I click on the "sub_changelang" button, it should change the program language to French for example. I got the following code to change the locale but I have no idea how to refresh/ pdate the app to change the language to French.
Button cl = (Button) findViewById(R.id.sub_changelang);
cl.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Locale locale = new Locale("fr_FR");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
}
});
It doesn't work. How can I fix it? I tried to add:
MainActivity.this.getResources().updateConfiguration(config, MainActivity.this.getResources().getDisplayMetrics());
but it didn't work. I also tried:
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
and it didn't work either.
android:configChanges="locale"
is set inside the AndroidMainfest.xml under application -> activity
I am using this code to set locale
String languageToLoad = "fr_FR";
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
context.getResources().updateConfiguration(config,context.getResources().getDisplayMetrics());
Intent intent = new Intent(XYZ.this, XYZ.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
here context is application Base Context.
Please also try "fr" instead of "fr_FR" because I am working for Arabic locale and its working fine.
You need to restart your Activity after changing locale.
You can use activity.this.recreate().But it will support from API level 11.
I want to let the user change the language of my application using spinner (or any way).
I tried many ways but they change the language of this activity not all activities, and I want to save it so when the user restart the app he will find the last choosed language.
you can use this code in spinner or any way you want
String languageToLoad = "en"; // your language
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
then you should save the language like this
SharedPreferences languagepref = getSharedPreferences("language",MODE_PRIVATE);
SharedPreferences.Editor editor = languagepref.edit();
editor.putString("languageToLoad",languageToLoad );
editor.commit();
and use the same code in every activity in onCreate() to load the languageToLoad from the SharedPreferences
This is an old question, but I'll answer it anyway :-)
You can extend Application class to apply Abol3z's solution on every Activity. Create class:
public class MyApplication extends Application {
#Override
public void onCreate() {
super.onCreate();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String lang = preferences.getString("lang", "en");
Locale locale = new Locale(lang);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
}
}
And set MyApplication as application class in manifest:
<application
android:name=".MyApplication"
...
/>
You can set the lang value (in your spinner):
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getContext());
preferences.edit().putString("lang", "en").commit();
Use SharedPreferences to keep track of the language the user chose, and then set the activities to use that language in the onCreate (), and maybe onResume() method. This way it will persist across app restarts etc.
btnChange.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mContext);
//preferences.edit().putString("lang", "bn").commit();
String lang = preferences.getString("lang", "en");
//Log.e("lang", "lang in Main Activity:"+lang);
if (lang.equalsIgnoreCase("en")){
setLocale("bn");
preferences.edit().putString("lang", "bn").commit();
btnChange.setText("Eng");
}else if(lang.equalsIgnoreCase("bn")){
setLocale("en");
preferences.edit().putString("lang", "en").commit();
btnChange.setText("বাংলা");
}
}
});
public void setLocale(String lang) {
myLocale = new Locale(lang);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
Intent refresh = new Intent(this, MainActivity.class);
startActivity(refresh);
finish();
}
we use two language for test purpose. keep all string in different folder named values and values-bn.