4

I am trying to provide a language setting(English, Traditional Chinese and Simplified Chinese) in the Android app but when the language preference is changed, the app does not have any change, it's still showing the same language. I have referred similar questions in stackoverflow but I have still no idea where my problem is. I would like to seek some helps for locating the errors. Thank you so much for your help!

public class AppPrefActivity extends Activity {

private SharedPreferences.OnSharedPreferenceChangeListener prefListener;
SharedPreferences pref;

public static final String PREF_LANGUAGE = "language_pref";

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    getFragmentManager().beginTransaction().replace(android.R.id.content, new PrefsFragement()).commit();

 }

public static class PrefsFragement extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preference);
    }

    @Override
    public void onResume() {
        super.onResume();
        getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onPause() {
        getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
        super.onPause();
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        if(key.equals(PREF_LANGUAGE))
        {
            Preference connectionPref = findPreference(key);
            connectionPref.setSummary(sharedPreferences.getString(key, ""));
            changeLanguagePref(sharedPreferences.getString(key, ""));
        }
    }

            private void changeLanguagePref(Context context, String lang){
        Locale locale = null;
        if (lang.equals("Traditional Chinese")){
            locale = new Locale("zh");//("zh_rTW");
        }else if (lang.equals("Simplified Chinese")){
            locale = new Locale("zh");//("zh_rCN");
        }else{
            locale = new Locale("en");
        }
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        context.getResources().updateConfiguration(config, null);
    }

}

}

AndroidManifest.xml

                <activity
        android:name=".AppPrefActivity"
        android:label="AppPrefActivity"
        android:configChanges="locale">
        <intent-filter>
            <action android:name="com.proj.george.betslog.AppPrefActivity" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

Preference array settings

<string-array name="app_language">
    <item>Traditional Chinese</item>
    <item>Simplified Chinese</item>
    <item>English</item>
</string-array>
George
  • 169
  • 1
  • 2
  • 10
  • Quick note: I think you have to reload the fragment (or move onto a different fragment/activity) to view the changes. It won't change automatically if I recall correctly. – Matter Cat Jul 16 '15 at 02:43
  • do you mean adding "getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);" in onCreate method? – George Jul 16 '15 at 16:28

1 Answers1

2

You need to do 2 things:

  1. Add following in your manifest:

    android:configChanges="locale"

  2. Create one method in your setting screen where user change language:

    public static void updateLanguage(Context context, String selectedLanguage) {
       if (!"".equals(selectedLanguage)) {
          if ("English".equals(selectedLanguage)) {
              selectedLanguage = "en";
          } else if ("Traditional Chinese".equals(selectedLanguage)) {
              selectedLanguage = "zh";
          }
          Locale locale = new Locale(selectedLanguage);
          Locale.setDefault(locale);
          Configuration config = new Configuration();
          config.locale = locale;
          context.getResources().updateConfiguration(config, null);
       }
    }
    
Ehsan Shadi
  • 609
  • 6
  • 17
Kinnar Vasa
  • 397
  • 1
  • 9
  • I have changed the folder name from value-zh-rCN to value-zh in order to test the code. But it still cannot change the language after the setting is changed. – George Jul 16 '15 at 16:29