I have to change the language on runtime in Android (yes, I know that this is not a good behaviour, but this is a requirement...).
So I have a basic class, from which every activity extends. This class has the following function:
public static void changeLanguage(Context context) {
Resources res = context.getResources();
/*
* Change locale settings in the app.
*/
DisplayMetrics dm = res.getDisplayMetrics();
/*
* Store and load data in this preferences
*/
android.content.res.Configuration conf = res.getConfiguration();
String[] localArray = res.getStringArray(R.array.language_short_array);
if (localArray != null) {
SharedPreferences settings = context.getSharedPreferences(
MyService.APP_ID, MODE_PRIVATE);
conf.locale = new Locale(localArray[settings.getInt(
PREFERED_LANGUAGE_KEY, 0)]);
res.updateConfiguration(conf, dm);
}
}
I will call this method in onCreate:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
changeLanguage(this);
}
This is in the super-class. My Activities extends from it and call super.onCreate at first. After this call, they set their layout and initializes their settings...
I thought that my lines of code would make it. But I have the following problem: Sometimes, the activity changes the language and sometimes not!
If I set a debug breakpoint on it and after the programm pauses I press continue, everything works fine. So I think, in some cases, where my Application is "slow enough", the language will change correctly, whereas the language won't change if the application is too fast...
Is there any solution of my problem? How can I be sure, that my language will change correctly in any time?
Thanks a lot!
Edit: Here is an example for a class which extends from my super-class
public class MainMenuActivity extends BaseActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
}
}
changeLanguage(this); only needs to be called when the language has changed or when the App is loaded.. res.updateConfiguration(conf, dm); updates the global config and is specific to your app instance not your activity instance.
When you change the locale in an Activity you have to recreate that Activity to see your language change. This can be easily done by forcing an orientation change then forcing it back like this:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
If you hit back after a language change you will see the old language because onCreate is not called. You will have to detect in onResume that the language changed and force a recreate of the Activity.
-= EDIT =-
Using Screen Orientation to reload the Activity has proven to be a bit buggy on some devices. I am now using this to reload the current Activity:
public static void resetScreen(Activity activity) {
if (activity != null) {
if (Build.VERSION.SDK_INT >= 11) {
activity.recreate();
} else {
Intent intent = activity.getIntent();
activity.overridePendingTransition(0, 0);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
activity.finish();
activity.overridePendingTransition(0, 0);
activity.startActivity(intent);
}
}
}
I think you need to post an example of an Activity that extends your superclass. Calling changeLanguage in onCreate() seems suspicious to me, though. That's only going to run when the app is first initialized. To change the language once your app is loaded, you'd have to stop it and re-create it.
Related
I am using the AppCompatDelegate.setDefaultNightMode(mode); to set the Night Mode in my Android Application, whenever the user chooses any mode that preference configuration in the Shared Preferences on their device, now while I use the shared preferences to set the UI mode when the app starts from the Splash Screen Activity, the activity is getting recreated and then there are 2 instances of my app, as the Splash Screen intents to Landing Activity.
Here is my SplashScreen.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
SharedPreferences prefs = getSharedPreferences(UI_MODE, MODE_PRIVATE);
name = prefs.getString("uiMode", "System");
applyUI();
fireSplashScreen();
}
private void applyUI() {
if (name.equals("Dark")){
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
}
else if (name.equals("Light")){
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
}
private void fireSplashScreen() {
Intent i = new Intent(SplashScreen.this, Landing.class);
startActivity(i);
finish();
}
What can I do to avoid creating multiple instances of the Landing Activity?
Make sure to call AppCompatDelegate.setDefaultNightMode() as soon as possible. E.g. before calling super.onCreate(). Ideally, you would want to call it in Application.onCreate(). Also, make sure to use the latest version of AppCompat (at least 1.1.0) or you might face this issue anyway.
See this answer for more details.
I'm trying to change the language of the application according to the user's input. I tried using this code to change the language of the application and it's working pretty fine.
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(MainActivity.this, MainActivity.class);
startActivity(refresh);
}
But the problem is that app has to restart/refresh in order to reload the resources.
Is this the correct approach to set the language of the app programmatically?
Is there any other way to change the language without refreshing the app?
Try with recreate() on your activity. This approach was successful in my case. If you are on Fragment, then use getActivity().recreate();
SharedPreferences.Editor editor = prefs.edit();
editor.putString(Constants.APP_STATE.SAVED_LOCALE, localeString);
editor.apply();
getActivity().recreate();
Override following method of your activity:
#Override
protected void attachBaseContext(Context newBase) {
SharedPreferences prefs = newBase.getSharedPreferences(Constants.APP_STATE.STATE_SHARED_PREFERENCES, MODE_PRIVATE);
String localeString = prefs.getString(Constants.APP_STATE.SAVED_LOCALE, Constants.DEFAULTS.DEFAULT_LOCALE);
Locale myLocale = new Locale(localeString);
Locale.setDefault(myLocale);
Configuration config = newBase.getResources().getConfiguration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
config.setLocale(myLocale);
if(Build.VERSION.SDK_INT > Build.VERSION_CODES.N){
Context newContext = newBase.createConfigurationContext(config);
super.attachBaseContext(newContext);
return;
}
} else {
config.locale = myLocale;
}
super.attachBaseContext(newBase);
getResources().updateConfiguration(config, getResources().getDisplayMetrics());
When user set wanted locale, what you want to do is to save it into some string in SharedPreferences and call recreate() of activity. This will then call attachBaseContext(Context context) and in this method proper locale will be set to configuration, then new context will be created with this configuration. After that, new context will be sent to super class which will update application context and proper locale will be shown.
It is also good because locale is automatically set when starting app next time.
Change locale is a Configuration which leads your acitivity restart. There are many other things have the same effect like orientation , keyboardHidden. Take care of your activity's states.
If restarting your activity requires that you recover large sets of
data, re-establish a network connection, or perform other intensive
operations, then a full restart due to a configuration change might be
a slow user experience. Also, it might not be possible for you to
completely restore your activity state with the Bundle that the system
saves for you with the onSaveInstanceState() callback—it is not
designed to carry large objects (such as bitmaps) and the data within
it must be serialized then deserialized, which can consume a lot of
memory and make the configuration change slow. In such a situation,
you can alleviate the burden of reinitializing part of your activity
by retaining a Fragment when your activity is restarted due to a
configuration change. This fragment can contain references to stateful
objects that you want to retain.
Not the best-practice, but you can handle it
<activity android:name=".MyActivity"
android:configChanges="locale"
android:label="#string/app_name">
Then in your Activity
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Do your stuff here
}
Remember to take care of your state. Using setRetainFragment(true) is a good approach. Read this
Yes your code is correct and if you want without refreshing the application
then In Your Application class you need to call this in onCreate() method
String languageSelected = "en";//selected language
Locale myLocale = new Locale(languageSelected);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
getActivity().onConfigurationChanged(conf);//Call this method
You need to perform this particular language to be selected on Application class.
I'm trying to implement app language switch on the runtime, once the user made language changes in app preferences. I have this code in my PreferenceFragment:
public class Fragment_Preferences extends PreferenceFragment {
private SharedPreferences.OnSharedPreferenceChangeListener prefListener;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
prefListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
Log.i("Pref changed", "Settings key changed: " + key);
if(key.equals("language_preference"))
{
String system_language = Locale.getDefault().getLanguage().toUpperCase();
String preference_language = Common_Methods.get_preference_language(getActivity());
Toast.makeText(getActivity(), "Pref changed: "+preference_language, Toast.LENGTH_SHORT).show();
Common_Methods.set_app_interface_language(getActivity(), system_language, preference_language);
}
}
};
prefs.registerOnSharedPreferenceChangeListener(prefListener);
}
}
This is my set_app_interface_language method in Common_Methods class:
public static void set_app_interface_language(Context context, String system_language, String preference_language)
{
if(!preference_language.equals(system_language))
{
Locale locale = new Locale(preference_language.toLowerCase());
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
}
}
I get the Toast message when I change language in preferences. I know that this method works since I call it also from my Fragment_Main. But the language doesn't change on the runtime - I have to exit the app and reopen it, only then I see the changes.
So how can I make the app language change on the runtime, without restarting the app? Thanks!
OK, I think I solved this problem: I call for Common_Methods.set_app_interface_language not in SharedPreferences.OnSharedPreferenceChangeListener, but rather in onRestart method of my Fragment_Main.
I also changed set_app_interface_language to return new config. And once it's returned - I pass it to onConfigurationChanged method to recreate fragment. Now, I did ran into Performing pause of activity that is not resumed... error message once my device's screen turned off. After googling a little about it, I realized that it's non-fatal exception, but I still used Handler to postpone recreate() for 1 millisecond and let the Fragment restart properly. I also set another method in my Common_Methods to check if there were any changes made to the app language and recreate the fragment only if the method returns true; I call this method in onRestart That gave the app some performance boost, since now there's no need to recreate the fragment every time the app restarts.
I have an app where the user can change its language.
Everything is working fine, with just this code on my MainActivity.onCreate():
String lang = PreferenceManager.getDefaultSharedPreferences(this).getString("languagePref", "default");
Configuration config = getResources().getConfiguration();
if( lang.equals("default") ) lang = Locale.getDefault().getLanguage();
config.locale = new Locale(lang);
getResources().updateConfiguration(config, getResources().getDisplayMetrics());
When I restart the app or navigate through activities it's still in the right language.
The only problem is on the PreferenceActivity screen. When the orientation changes, the PreferenceActivity title (and only it) changes to the default device language.
The prefs are still checked correctly, if I go back (closing the PreferenceActivity) the app is still on the right language, but the PreferenceActivity stays wrong until I restart the app.
I tried forcing the code above on the PreferenceActivity.onCreate() and altough debugging seems OK, the PrefenceActivity Title stays wrong.
Here's my PrefenceActivity code:
public class PreferencesActivity extends PreferenceActivity {
#SuppressWarnings("deprecation")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
This behavior doesn't happen on any other Activity :/
Locking the screen orientation is not an option.
Any thoughts?
Thanks.
OK, this fixed it for me.
#Override
protected void onSaveInstanceState(Bundle outState) {
String lang = PreferenceManager.getDefaultSharedPreferences(this).getString("languagePref", "default");
Configuration config = getResources().getConfiguration();
if( lang.equals("default") ) lang = Locale.getDefault().getLanguage();
config.locale = new Locale(lang);
getResources().updateConfiguration(config, getResources().getDisplayMetrics());
super.onSaveInstanceState(outState);
}
I solved this problem using onConfigurationChanged().
In this method I'm saving preferred language again in Configuration object.
OnCreate method of activity is called when orientation is changes, so some property is again set by your code, look at your on oncreate method or lock your screen orientation.
You can lock your screen orientation to your activity by Using
android:screenOrientation="portrait"
or
android:screenOrientation="landscape"
I already know how to change the language of my application (updating the configuration). My code also check if the configuration is changed by the system and "fix it" in the ´onCreate´ method. I even have created a ListPreference to let the user decide the language with one that my app supports (and saves the decision).
Let's say I have 3 activities (A, B and SettingsActivity). Activity A can start activities B and SettingsActivity. Activity B can start SettingsActivity. If the user changes the language inside SettingsActivity, I can update its resources (in this case Strings) without any problem using this code:
//if (Build.VERSION_CODES.HONEYCOMB <= Build.VERSION.SDK_INT) {
// Disabled because it blinks and looks bad
// recreate();
// } else {
startActivity(getIntent());
finish();
overridePendingTransition(0, 0);
// }
However, I'm unable to change the already open activities because I have no reference to them from SettingsActivity.
My question: is there any clean way to update the resources or recreate the already open activities? If I don't find a better solution, my approach will be one of the above:
Start activities using startActivityForResultand return a code to trigger the code I already use to recreate the activity.
Check inside the onResume method if the current language has changed and do the same thing.
At the end what I did was this:
#Override
protected void onStart() {
super.onStart();
if (!locale.equals(getResources().getConfiguration().locale)) {
finish();
startActivity(getIntent());
overridePendingTransition(0, 0);
return;
}
}
Where locale is a variable assigned in my onCreate method:
private Locale locale;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
((Application) getApplication()).refreshLanguage();
locale = getResources().getConfiguration().locale;
setContentView(R.layout.activity_main);
//moar code
}
Finally, for the sake of posting code, here is my refreshLanguage method(s):
boolean refreshLanguage() {
return refreshLanguage(PreferenceManager.getDefaultSharedPreferences(this));
}
boolean refreshLanguage(SharedPreferences sharedPreferences) {
if (sharedPreferences.contains("language")) {
int languageIndex = Integer.parseInt(sharedPreferences.getString("language", "0"));
if (!getResources().getConfiguration().locale.equals(languages[languageIndex])) {
Configuration config = new Configuration();
config.locale = languages[languageIndex];
getResources().updateConfiguration(config, null);
return true;
}
}
return false;
}
Notice that I use onStart rather than onResume because I'm not switching between transparent activities or using dialogs.