Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid change local configuration doesn't work
    text
    copied!<p>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.</p> <h3>My problem</h3> <p>when I launch the application, It always launches in the <code>English</code> language, although I print the locale language and the result is <code>Arabic</code>.</p> <h3>Hint</h3> <p>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.</p> <h3>My code</h3> <p><strong>Tags.java</strong> - A class which contains but three strings:</p> <pre><code>public class Tags { public final static String LANGUAGE = "Language"; public final static String ENGLISH_LANGUAGE = "en"; public final static String ARABIC_LANGUAGE = "ar"; } </code></pre> <p><strong>Splash activity</strong> - The activity where the application starts:</p> <pre><code>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(); </code></pre> <p>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 <code>Setting activity</code>.</p> <p>I am sure that you will ask me to give you the code of the <code>Setting activity</code> in order to compare what I have done in that activity with the splash activity. This code is as follows:</p> <p><strong>Setting activity</strong></p> <pre><code>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(); </code></pre> <p>The code is pretty simple, I used a spineer <code>s_language</code> from two values.</p> <p><em>A problem could be that, in the setting activity, when the user presses <code>save</code>, the <code>Setting</code> activity is finished. In the <code>splash</code> activity, I check for the language in the <code>oncreate</code> 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.</em></p> <p>How can I resolve this issue?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload