Skip to content Skip to sidebar Skip to footer

After The Screen Rotation, The Language Of My Application Will Be Changed

I have created a bilingual (having two languages) android application. I have inserted my resource strings in two files: For Persian language (default) values/strings_locale.xml‬

Solution 1:

You can make class which extends Application. There you can override one method which gets called everytime you change configuration (example when screen orientation gets changed).

Something like:

publicclassMyApplicationextendsApplication {
    @OverridepublicvoidonConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        setLocale();
    }

    privatevoidsetLocale() {
        Localelocale=newLocale("fa_IR");
        Locale.setDefault(locale);
        Configurationconfig=newConfiguration();
        config.locale = locale;
        getBaseContext().getResources().updateConfiguration(config,
              getBaseContext().getResources().getDisplayMetrics());
    }
}

And dont forget to declare it in the manifest: example of Application class

In AndroidManifest.xml:

<application
    android:name="com.blaablaa.bumbam.MyApplication"
    ...

Solution 2:

I don't exactly understand what you are trying to do. But in general you don't have to manage the language of the app. Android will automatically pick the phone language if available and in any other case bring up a dialog to pick the language.

If you do want to set language independent from that with your code - which I would not recommend - then there is probably a problem with your lifecycle preventing your code from being run again after an orientation change. I can help you with that if you post more of your code.

Solution 3:

You can create custom ContextWrapper

publicclassmContextWrapperextendsContextWrapper {
    publicmContextWrapper(Context base){
        super(base);
    }

    @SuppressWarnings("deprecation")
    publicstaticContextWrapperwrap(Context context, String language) {
        Configuration config = context.getResources().getConfiguration();
        Locale sysLocale = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            sysLocale = getSystemLocale(config);
        } else {
            sysLocale = getSystemLocaleLegacy(config);
        }
        if (!language.equals("") && !sysLocale.getLanguage().equals(language)) {
            Locale locale = newLocale(language);
            Locale.setDefault(locale);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                setSystemLocale(config, locale);
            } else {
                setSystemLocaleLegacy(config, locale);
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                context = context.createConfigurationContext(config);
            } else {
                context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
            }
        }
        returnnewmContextWrapper(context);
    }

    @SuppressWarnings("deprecation")
    publicstaticLocalegetSystemLocaleLegacy(Configuration config){
        return config.locale;
    }

    @TargetApi(Build.VERSION_CODES.N)
    publicstaticLocalegetSystemLocale(Configuration config){
        return config.getLocales().get(0);
    }

    @SuppressWarnings("deprecation")
    publicstaticvoidsetSystemLocaleLegacy(Configuration config, Locale locale){
        config.locale = locale;
    }

    @TargetApi(Build.VERSION_CODES.N)
    publicstaticvoidsetSystemLocale(Configuration config, Locale locale){
        config.setLocale(locale);
    }
}

and in your activity attachBaseContext attach your custom ContextWrapper

@OverrideprotectedvoidattachBaseContext(Context newBase) {
        globalClass = (YouAppClass)newBase.getApplicationContext();
        //try geting the lang you have setted inside your YouAppClass class// or you can use shared preferences for it //pref = PreferenceManager.getDefaultSharedPreferences(newBase)
        lang=globalClass .getLang();
        super.attachBaseContext(mContextWrapper.wrap(newBase,lang));
    }

Post a Comment for "After The Screen Rotation, The Language Of My Application Will Be Changed"