Reputation: 1183
I'm choosing the language of my app in the MainActivity.java
and sometimes it doesn't work properly.
This is my code on the onCreate
method:
if(!SaveSharedPreference.getUserId(MainActivity.this).equals("")) {
if (SaveSharedPreference.getWhoLoggedIn(MainActivity.this).equals("Main_User")) {
FetchUserLanguage fetchUserLanguage = new FetchUserLanguage();
fetchUserLanguage.execute();
}
} else {
sDefSystemDefaultLanguage = Resources.getSystem().getConfiguration().locale;
Configuration configuration = new Configuration();
configuration.locale = sDefSystemDefaultLanguage;
getBaseContext().getResources().updateConfiguration(configuration, getBaseContext().getResources().getDisplayMetrics());
}
In the FetchUserLanguage
I'm getting the language for each user from my database (it is returning the value correctly).
The method where I set the Locale
is the next:
@Override
protected void onPostExecute(String result) {
switch (result) {
case "English":
locale = new Locale("en");
configuration = new Configuration();
configuration.locale = locale;
getBaseContext().getResources().updateConfiguration(configuration,
getBaseContext().getResources().getDisplayMetrics());
break;
case "Deutsch":
locale = new Locale("de");
configuration = new Configuration();
configuration.locale = locale;
getBaseContext().getResources().updateConfiguration(configuration,
getBaseContext().getResources().getDisplayMetrics());
break;
default:
locale = Resources.getSystem().getConfiguration().locale;
configuration = new Configuration();
configuration.locale = locale;
getBaseContext().getResources().updateConfiguration(configuration,
getBaseContext().getResources().getDisplayMetrics());
break;
}
}
When I run the app from the Android Studio it works perfect and all the app is set in the selected language, but then, if I close and open it again, the first screen is being shown in English, (the other are in German). The language of the device is English so do the default language of the Android project.
Maybe I'm missing something.
Thank you very much.
Upvotes: 0
Views: 1964
Reputation: 1183
Finally I´ve found the solution.
The problem was that I was doing the translation process in an asynchronous thread, so the app was still running and some of the layouts weren't changed to the new language before the screen changed.
The solution is to create the Intent at the end of the onPostExecute()
method:
@Override
protected void onPostExecute(String result) {
switch (result) {
case "English":
locale = new Locale("en");
Locale.setDefault(locale);
configuration = new Configuration();
configuration.locale = locale;
SaveSharedPreference.setUserLanguage(MainActivity.this, locale.getLanguage());
getBaseContext().getResources().updateConfiguration(configuration,
getBaseContext().getResources().getDisplayMetrics());
break;
case "Deutsch":
locale = new Locale("de");
Locale.setDefault(locale);
configuration = new Configuration();
configuration.locale = locale;
SaveSharedPreference.setUserLanguage(MainActivity.this, locale.getLanguage());
getBaseContext().getResources().updateConfiguration(configuration,
getBaseContext().getResources().getDisplayMetrics());
break;
default:
locale = Resources.getSystem().getConfiguration().locale;
Locale.setDefault(locale);
SaveSharedPreference.setUserLanguage(MainActivity.this, locale.getLanguage());
configuration = new Configuration();
configuration.locale = locale;
getBaseContext().getResources().updateConfiguration(configuration,
getBaseContext().getResources().getDisplayMetrics());
break;
}
Intent intent = new Intent(MainActivity.this, MainMenuActivity.class);
if(SaveSharedPreference.getUserType(MainActivity.this).equals("Full")) {
intent.putExtra("Type", "Full");
}
startActivity(intent);
finish();
}
Upvotes: 1