Skip to content

Instantly share code, notes, and snippets.

Last active April 8, 2021 12:34
Show Gist options
  • Save gunhansancar/45648176dc47d50b1940 to your computer and use it in GitHub Desktop.
Save gunhansancar/45648176dc47d50b1940 to your computer and use it in GitHub Desktop.
While developing your awesome application, sometimes you are required to add a language change feature to your app on the fly. However, Android OS does not directly support this behaviour. And therefore, you need to solve this situation in some other ways. For more details see
package com.gunhansancar.changelanguageexample.helper;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.preference.PreferenceManager;
import java.util.Locale;
* This class is used to change your application locale and persist this change for the next time
* that your app is going to be used.
* <p/>
* You can also change the locale of your application on the fly by using the setLocale method.
* <p/>
* Created by gunhansancar on 07/10/15.
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);
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);
private static Context updateResources(Context context, String language) {
Locale locale = new Locale(language);
Configuration configuration = context.getResources().getConfiguration();
return context.createConfigurationContext(configuration);
private static Context updateResourcesLegacy(Context context, String language) {
Locale locale = new Locale(language);
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
configuration.locale = locale;
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
return context;
Copy link

prcGit commented Jan 9, 2018

any idea how to change locale on android O? createConfigurationContext is not working for me...

Copy link

is not working for me

Copy link

Flimm commented Oct 25, 2018

Is this code licensed under an open source license?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment