Matin Soleimani
Matin Soleimani

Reputation: 77

Android - Changing app language for all versions of Android

I tried a few ways to change the locale of the android app, but when I test my app on Android 7.0 or lower, the locale is not changed.

Note: The ways worked on Android 8.0 or upper, but they didn't work on Android 7.0 or lower.

Which way does work on all of the Android versions? Do you know?

The ways that I tried:

Note: I tested my app on Galaxy Tab S2 with Android 7.0 (Didn't work). And on a Galaxy A5 2017 with Android 8.0 (Worked).

Note: Many of my activities wrote in Kotlin.

My codes:

Locale Helper class:

public class LocaleHelper {

private static final String SELECTED_LANGUAGE = "Locale.Helper.Selected.Language";

public static Context onAttach(Context context) {
    String lang = getPersistedData(context, Locale.getDefault().getLanguage());
    return setLocale(context, lang);
}

public static Context onAttach(Context context, String defaultLanguage) {
    String lang = getPersistedData(context, defaultLanguage);
    return setLocale(context, lang);
}

public static String getLanguage(Context context) {
    return getPersistedData(context, Locale.getDefault().getLanguage());
}

public static Context setLocale(Context context, String language) {
    persist(context, language);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        return updateResources(context, language);
    }

    return updateResourcesLegacy(context, language);
}

private static String getPersistedData(Context context, String defaultLanguage) {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    return preferences.getString(SELECTED_LANGUAGE, defaultLanguage);
}

private static void persist(Context context, String language) {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor editor = preferences.edit();

    editor.putString(SELECTED_LANGUAGE, language);
    editor.apply();
}

@TargetApi(Build.VERSION_CODES.N)
private static Context updateResources(Context context, String language) {
    Locale locale = new Locale(language);
    Locale.setDefault(locale);

    Configuration configuration = context.getResources().getConfiguration();
    configuration.setLocale(locale);

    return context.createConfigurationContext(configuration);
}

@SuppressWarnings("deprecation")
private static Context updateResourcesLegacy(Context context, String language) {
    Locale locale = new Locale(language);
    Locale.setDefault(locale);

    Resources resources = context.getResources();

    Configuration configuration = resources.getConfiguration();
    configuration.locale = locale;

    resources.updateConfiguration(configuration, resources.getDisplayMetrics());

    return context;
}

}

attachBaseContext method(I put these codes at all of my activities):

override fun attachBaseContext(base: Context?) {
    super.attachBaseContext(LocaleHelper.onAttach(base, "en"))
}

Changing locale method(To Persian):

LocaleHelper.setLocale(getContext(), "fa")

Upvotes: 2

Views: 192

Answers (1)

mostafa tayebi
mostafa tayebi

Reputation: 309

You should create a base activity for example and override onConfigurationChange method on that, set the default language that you saved in shared preferences. After all you have to extend your activities from base activity.

Upvotes: 3

Related Questions