Reputation: 329
In an activity I did the following to change the language of the app:
private void changeLanguage(String stringLanguage){
Locale locale = new Locale(stringLanguage);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,getBaseContext().getResources().getDisplayMetrics());
Intent refresh = new Intent(ActLanguage.this, ActLanguage.class);
startActivity(refresh);
finish();
}
But only that activity changes language, all the other activities that the project contains remain in the original language.
Any suggestions or comments are welcome.
Upvotes: 1
Views: 198
Reputation: 58934
Some workaround for changing app language would be.
This does not work because you did not call onConfigurationChanged()
.
This would be your application class. You can change language by calling method.
AppContext.getInstance().changeLanguage("en");
Intent refresh = new Intent(ActLanguage.this, ActLanguage.class);
finish();
startActivity(refresh);
and AppContext.java
class
import android.app.Application;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import java.util.Locale;
public class AppContext extends Application {
public static final String PREFERENCE = "NYPrefsFile";
public static final String KEY_LANG = "LANG";
private static AppContext mApp = null;
@Override
public void onCreate() {
super.onCreate();
mApp = this;
setLanguage();
sp = getSharedPreferences(PREFERENCE, 0);
}
public synchronized static AppContext getInstance() {
return mApp;
}
public void setLanguage() {
String lang = getStringData(KEY_LANG);
changeLanguage(lang);
}
public String getCurrentLanguage() {
if (config == null)
config = getApplicationContext().getResources().getConfiguration();
return config.locale.getLanguage();
}
Configuration config;
public void changeLanguage(String lang) {
if (config == null)
config = getApplicationContext().getResources().getConfiguration();
Locale locale = new Locale(lang);
Locale.setDefault(locale);
config.locale = locale;
getApplicationContext().getResources().updateConfiguration(config, getApplicationContext().getResources().getDisplayMetrics());
setStringData(KEY_LANG, lang);
onConfigurationChanged(config);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
private SharedPreferences sp;
public String getStringData(String pKey) {
return sp.getString(pKey, "");
}
public void setStringData(String pKey, String pData) {
SharedPreferences.Editor editor = sp.edit();
editor.putString(pKey, pData);
editor.apply();
}
}
In AndroidManifest.xml
<application
android:name=".AppContext"
Update:
You will start your home activity with clearing back-stack to load all activities with new language.
Intent intent = new Intent(this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish(); // call this to finish the current activity
Upvotes: 1
Reputation: 329
The solution was:
Intent refresh = new Intent(ActIdioma.this, ActMAINACTIVITY.class);
refresh.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(refresh);
finish();
Calling the main activity, all the others change language.
Upvotes: 0
Reputation: 300
Do if from the Application context and not from the Activity context. Use getApplicationContext() .
Also use createConfigurationContext from API 25 as updateConfiguration is deprecated .
Upvotes: 0