Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change App Language Settings using PreferenceFragment

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>
like image 240
George Avatar asked Oct 25 '25 01:10

George


1 Answers

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);
       }
    }
    
like image 135
Kinnar Vasa Avatar answered Oct 26 '25 15:10

Kinnar Vasa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!