android


Android - App locale changes and displayed orientation


I'm having a headache with the locale thing in my app. So basically my app supports two languages, English and Vietnamese, and user can choose to change the language to be displayed. So I have a SettingActivity like this:
public class SettingsActivity extends SherlockPreferenceActivity implements OnPreferenceChangeListener{
private ListPreference langPref;
private SharedPreferences languagepref;
private String language;
private Locale locale;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
languagepref = PreferenceManager.getDefaultSharedPreferences(this);
Configuration config = getBaseContext().getResources().getConfiguration();
language = languagepref.getString("languageToLoad","en");
locale = new Locale(language);
Locale.setDefault(locale);
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
addPreferencesFromResource(R.xml.preferences_login);
langPref = (ListPreference) findPreference("lang_pref");
if (language.equalsIgnoreCase("vi")){
langPref.setValueIndex(1);
}
else{
langPref.setValueIndex(0);
}
langPref.setOnPreferenceChangeListener(this);
}
#Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
String key = preference.getKey();
if (key.equals("lang_pref")){
String languageToLoad = (String) newValue;
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
SharedPreferences.Editor editor = languagepref.edit();
editor.putString("languageToLoad",languageToLoad );
editor.commit();
recreate();
}
return false;
}
}
In every other activity, I add this in their onCreate method:
SharedPreferences languagepref = PreferenceManager.getDefaultSharedPreferences(this);
Configuration config = getBaseContext().getResources().getConfiguration();
String language = languagepref.getString("languageToLoad","en");
Locale locale = new Locale(language);
Locale.setDefault(locale);
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
And now my app is acting weird: in the SettingActivity, the language is displayed properly whenever I rotate my device, even without the need of adding android:configChanges="locale" in the activity's manifest. However, in every other activity, the language is changed back to English when I rotate the device. I've try adding android:configChanges="locale" in their manifest but it doesn't work. Also, I've tried every solution found on Stackoverflow but none of them works for me, so I'm basically clueless now. Could anyone of you help me point out the mistake I made here? Please do and thanks in advance.
Try adding
android:configChanges="orientation|keyboard|keyboardHidden"
in the manifest for each activity.
Then you should override onConfigurationChanged in your activities, and in this method reload layout (depending orientation). If you still have problems with locale, reload the appropriate locale in onConfigurationChanged.

Related Links

Data not updating on pressing back button
Update specific Items in ListView
Android application hangup when i implemeting Retrofit2
how I can choose name of my app
Tab Layout in a Fragment
Undo delete listview/recycleview with realm object
How to fix error (1, -2147483648)
whatsapp and twitter is not getting images in android app
Android Text-to- Speech not working for loop
How can use dialog box with all list item in listview
how to send and receive data to server?
Android Studio changing background color from fragment not working?
How should I incorporate Like button in Android App with Facebook sdk 4?
Why android gradle task is starting before libs are built?
java.lang.NoClassDefFoundError: com.google.firebase.FirebaseOptions
Basic Firebase database save

Categories

HOME
service-worker
dns
gpu
rdbms
pyqt
case
obfuscation
oracle-apex-5
devise
error-handling
replace
pdfbox
simulink
jquery-ui
datastage
naivebayes
spring-social
selenium-ide
magnetic-cards
selectize.js
qsqlquery
http-post
screen-readers
gruntfile
glyphicons
soci
resourcebundle
ceylon
aws-codecommit
postback
wacom
hreflang
nested-lists
getlasterror
verifiable-c
tasm
portforwarding
microsoft-metro
android-checkbox
drupal-theming
importerror
optimizely
jide
servermanager
double-click
ntvs
reportservice2010
twilio-click-to-call
singleinstance
lsyncd
baidu
ampps
jboss-tools
jtwig
google-query-language
findfirst
themoviedb-api
xenomai
wiredep
ons-api
video-player
dnssec
aspectj-maven-plugin
structuremap3
tweenlite
named-parameters
android-framework
fpdi
solace-mq
c++builder-xe5
printdialog
flex++
resource-management
redis-py
onmousemove
qtconcurrent
kqueue
jsr286
jquery-address
denied
android-holo-everywhere
lr
mbeans
hbm2java
addchild
burndowncharts

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App