Reputation: 319
Is it possible to make Android app using language which is chosen by click on some of few buttons? For example, if I have four buttons on beginning of an app, with text: "english", "german", "italiano", "español", I want to use german language in an app when I click on button "german". I know I have to use different folders for strings(values-de for german), but I don't know how and where to change default setting which uses default folder for values. Thanks in advance!
Upvotes: 0
Views: 214
Reputation: 1558
Try below code.
for language change i made one function you have to pass language code and context.
language="en" set language as per your requirement(English en, French fr, Spanish sp & Other Languages) context pass your activity/fragment context
public static boolean setLangRecreate(String language, Context context) {
try {
Configuration config = context.getResources().getConfiguration();
Locale locale = new Locale(language);
Locale.setDefault(locale);
config.locale = locale;
context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
// recreate();
((Activity) context).finish();
((Activity) context).overridePendingTransition(0, 0);
((Activity) context).startActivity(((Activity) context).getIntent());
((Activity) context).overridePendingTransition(0, 0);
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;}
Upvotes: 1
Reputation: 8835
You need to override attachBaseContext method in every class. and pass the selected locale code to super method. The below code is used in Kotlin
override fun attachBaseContext(newBase: Context) {
val lang = SharedPref.getPrefVal<String>(newBase, Constants.SELECTED_LANG_CODE)
super.attachBaseContext(MyContextWrapper.wrap(newBase, lang))
}
MyContextWrapper class to support language change in all versions
public class MyContextWrapper extends ContextWrapper {
public MyContextWrapper(Context base) {
super(base);
}
@SuppressWarnings("deprecation")
public static ContextWrapper wrap(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 = new Locale(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());
}
return new MyContextWrapper(context);
}
@SuppressWarnings("deprecation")
public static Locale getSystemLocaleLegacy(Configuration config) {
return config.locale;
}
@TargetApi(Build.VERSION_CODES.N)
public static Locale getSystemLocale(Configuration config) {
return config.getLocales().get(0);
}
@SuppressWarnings("deprecation")
public static void setSystemLocaleLegacy(Configuration config, Locale locale) {
config.locale = locale;
}
@TargetApi(Build.VERSION_CODES.N)
public static void setSystemLocale(Configuration config, Locale locale) {
config.setLocale(locale);
}
}
and while setting the language in onClick method use:
try {
attachBaseContext(MyContextWrapper.wrap(applicationContext, lang_code))
} catch (e: Exception) {
}
Let me know if you need any help to understand the code structure
Upvotes: 1