after studying about supporting multiple languages in android application, i have good the basic idea about how i can make resources for different languages within application, for example i want to add Spanish language in my application so in res direction within my application i have added values-en directory and in that direct i have added strings resource and within that resource i have added strings with values of Spanish text, in my application default language is English now i want to know how can i switch it to Spanish, i have the resources ready i just need to change my application language to Spanish
Asked
Active
Viewed 958 times
0
-
Check this http://stackoverflow.com/questions/2900023/change-language-programmatically-in-android – Ninja Mar 25 '17 at 08:49
-
2`i want to add Spanish language in my application so in res direction within my application i have added values-en directory` **WRONG**: you should have added `values-es`. – Phantômaxx Mar 25 '17 at 08:49
1 Answers
0
Use below code in your onCreate()
after setContentView
:
String languageToLoad = "es";
Locale locale = new Locale(languageToLoad);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
You also have to change your value folder to values-es. values-en is for English. Hope this helps.
To change App locale follow below code:
1) Create LocaleUtils class:
public class LocaleUtils {
private static Locale sLocale;
public static void setLocale(Locale locale) {
sLocale = locale;
if(sLocale != null) {
Locale.setDefault(sLocale);
}
}
public static void updateConfig(ContextThemeWrapper wrapper) {
if(sLocale != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
Configuration configuration = new Configuration();
configuration.setLocale(sLocale);
wrapper.applyOverrideConfiguration(configuration);
}
}
public static void updateConfig(Application app, Configuration configuration) {
if (sLocale != null && Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
//Wrapping the configuration to avoid Activity endless loop
Configuration config = new Configuration(configuration);
// We must use the now-deprecated config.locale and res.updateConfiguration here,
// because the replacements aren't available till API level 24 and 17 respectively.
config.locale = sLocale;
Resources res = app.getBaseContext().getResources();
res.updateConfiguration(config, res.getDisplayMetrics());
}
}
}
2) In Application Class:
public class YourAppName extends Application {
public void onCreate(){
super.onCreate();
LocaleUtils.setLocale(new Locale("es"));
LocaleUtils.updateConfig(this, getBaseContext().getResources().getConfiguration());
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
LocaleUtils.updateConfig(this, newConfig);
}
}
3) Remember, Your Application class name and <application android:name=".YourAppName">
should be same. Otherwise it won't work. Kudos to this answer.

Community
- 1
- 1

tahsinRupam
- 6,325
- 1
- 18
- 34
-
it will change application language or current activity ? and should i do this in MainActivity or in Application Activity ? – user7661245 Mar 25 '17 at 08:53
-
It will change the language of current activity. Do you want to change language of whole app at a time? Then you have to configure the Application class. – tahsinRupam Mar 25 '17 at 08:55
-
this code executes well in MainAcitivty but when i execute it in Application class i get this error " Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference" – user7661245 Mar 25 '17 at 09:17