I am building an application which comes in two languages, using a setting activity where the user can change the language. Once the user has selected any language, I want to save his/her choice so when the application is launched again, the new language will appear. I have done everything connected to layouts in different languages and different orientations.
My problem
when I launch the application, It always launches in the English
language, although I print the locale language and the result is Arabic
.
Hint
when the user changes the language from the setting activity, and he/she presses save, the application is working perfectly in the new language. Unfortunately, when restarting the application, it is not started in this new language.
My code
Tags.java - A class which contains but three strings:
public class Tags {
public final static String LANGUAGE = "Language";
public final static String ENGLISH_LANGUAGE = "en";
public final static String ARABIC_LANGUAGE = "ar";
}
Splash activity - The activity where the application starts:
someData = getSharedPreferences(filename, 0);
android.content.SharedPreferences.Editor editor = someData.edit();
if (!someData.contains(Tags.LANGUAGE)) {
editor.putString(Tags.LANGUAGE, Tags.ENGLISH_LANGUAGE);
Locale locale = new Locale(Tags.ENGLISH_LANGUAGE);
Locale.setDefault(locale);
android.content.res.Configuration config = new android.content.res.Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
Setting.setLanguage(Tags.ENGLISH_LANGUAGE);
} else {
String currentLanguage = someData.getString(Tags.LANGUAGE, Tags.ENGLISH_LANGUAGE);
Setting.setLanguage(currentLanguage);
Log.e("currnetLanPHone", Setting.getLanguage());
if (currentLanguage.equalsIgnoreCase(Tags.ENGLISH_LANGUAGE)) {
Locale locale = new Locale(Tags.ENGLISH_LANGUAGE);
Locale.setDefault(locale);
android.content.res.Configuration config = new android.content.res.Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
} else {
Locale locale = new Locale(Tags.ARABIC_LANGUAGE);
Locale.setDefault(locale);
android.content.res.Configuration config = new android.content.res.Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
}
}
editor.commit();
It confuses me because as I explained before, the language of the application is changed and the new layouts are loading when I change the language from the Setting activity
.
I am sure that you will ask me to give you the code of the Setting activity
in order to compare what I have done in that activity with the splash activity. This code is as follows:
Setting activity
if (s_language.getSelectedItem().toString().equalsIgnoreCase("English")) {
editor.putString(Tags.LANGUAGE, Tags.ENGLISH_LANGUAGE);
Locale locale = new Locale(Tags.ENGLISH_LANGUAGE);
Locale.setDefault(locale);
Log.e("Inside", Tags.ENGLISH_LANGUAGE);
android.content.res.Configuration config = new android.content.res.Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
Setting.setLanguage(Tags.ENGLISH_LANGUAGE);
} else {
editor.putString(Tags.LANGUAGE, Tags.ARABIC_LANGUAGE);
Locale locale = new Locale(Tags.ARABIC_LANGUAGE);
Locale.setDefault(locale);
android.content.res.Configuration config = new android.content.res.Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
Setting.setLanguage(Tags.ARABIC_LANGUAGE);
}
editor.commit();
The code is pretty simple, I used a spineer s_language
from two values.
A problem could be that, in the setting activity, when the user presses save
, the Setting
activity is finished. In the splash
activity, I check for the language in the oncreate
function, before continuing the activity and finishing it after doing a number of steps. I don't think that finishing the activity is the problem, but I just told you in order to make the image clear.
How can I resolve this issue?