简体   繁体   中英

Android locale gets changed when the orientation is changed to landscape

I am creating an android application in which i have set the locale for english and tamil language when i click the tamil language it displays the strings in tamils and every thing works fine.I am having an activity which is in landscape mode when the start that activity the locale gets changed from tamil to english even i have used

   android:configChanges="locale|orientation|screenSize|keyboardHidden" 

in manifest but it didn't works.

Can anyone tell me how i can overcome this issue.I have store the language in shared preference.

 private static MyApplication mInstance;
public static final String PREF_NAME = "CoconutMetaData";
public static final String PREF_USER_LANGUAGE_KEY = "userLanguage";
@Override
public void onCreate() {
    super.onCreate();

    mInstance = this;
    SharedPreferences preferences = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
    String myLanguage = preferences.getString(PREF_USER_LANGUAGE_KEY,"en");

    // Set user specific locale (Language)
    Locale mLocale = new Locale(myLanguage);
    Resources res = getResources();
    DisplayMetrics dm = res.getDisplayMetrics();
    Configuration conf = res.getConfiguration();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        conf.setLocale(mLocale);
    }
    res.updateConfiguration(conf, dm);

    /**
     * To display overflow menu when hard menu key is pressed (for devices that have hard menu key)
     */
    try {
        ViewConfiguration config = ViewConfiguration.get(this);
        Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
        if (menuKeyField != null) {
            menuKeyField.setAccessible(true);
            menuKeyField.setBoolean(config, false);
        }
    } catch (Exception ex) {

    }
}

public static synchronized MyApplication getInstance() {
    return mInstance;
}

public void setConnectivityListener(ConnectivityReceiver.ConnectivityReceiverListener listener) {
    ConnectivityReceiver.connectivityReceiverListener = listener;
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
}

Declare Local and Configuration object globally

    Locale mLocale;
    Configuration conf;

and set Local from

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    try {
         conf.setLocale(mLocale);
    } catch (Exception e) {
        // TODO: handle exception
    }
}

try following code, this is good way to set local language

public void setLocale(String lang) {

    myLocale = new Locale(lang);
    Resources res = getResources();
    DisplayMetrics dm = res.getDisplayMetrics();
    Configuration conf = res.getConfiguration();
    conf.locale = myLocale;
    res.updateConfiguration(conf, dm);
    Intent refresh = new Intent(this, AndroidLocalize.class);
    startActivity(refresh);
}

Example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM