I have saved both language xml file in res > values > strings. When a radiogroup button of a language is checked it should change the app language into nepali language and it should save preference also but it didn’t worked and the RadioGroup is checked in both at same time also.
private static final String PREFS_NAME = "app_prefs";
private static final String KEY_LANGUAGE = "language";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_language);
RadioGroup radioGroup = findViewById(R.id.radioGroup);
String savedLanguage = PreferenceManager.getLanguagePreference(this);
if (savedLanguage.equals("en")) {
radioGroup.check(R.id.englishButton);
} else if (savedLanguage.equals("ne")) {
radioGroup.check(R.id.nepaliButton);
}
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton selectedRadioButton = findViewById(checkedId);
String languageCode = selectedRadioButton.getTag().toString();
LocaleHelper.setLocale(LanguageActivity.this, languageCode);
PreferenceManager.saveLanguagePreference(LanguageActivity.this, languageCode);
}
});
public static class LocaleHelper {
public static void setLocale(Context context, String languageCode) {
Locale locale = new Locale(languageCode);
Locale.setDefault(locale);
Resources resources = context.getResources();
Configuration config = resources.getConfiguration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
config.setLocale(locale);
context.createConfigurationContext(config);
} else {
config.locale = locale;
resources.updateConfiguration(config, resources.getDisplayMetrics());
}
}
}
public static class PreferenceManager {
public static void saveLanguagePreference(Context context, String languageCode) {
SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(KEY_LANGUAGE, languageCode);
editor.apply();
}
public static String getLanguagePreference(Context context) {
SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
return prefs.getString(KEY_LANGUAGE, "en");
}
}
Is there problem with code or app didn’t found xml file placement.
try below class (I have only Kotlin version), call applySavedLanguage
in onCreate
of overriden Application
class
object LanguageUtil {
private fun updateLanguage(language: String) {
var newLocale: LocaleListCompat = LocaleListCompat.forLanguageTags(language)
if (newLocale.isEmpty) {
newLocale =
LocaleListCompat.getEmptyLocaleList() // fallback for "auto" or any other unknown
}
AppCompatDelegate.setApplicationLocales(newLocale)
}
fun applySavedLanguage(context: Context) {
val savedLanguage: String = "fr" //put own, get from sharedPrefs using ctx, or pass argument
val locales = AppCompatDelegate.getApplicationLocales()
for (i in 0 until locales.size()) {
if (locales[i]?.language?.startsWith(savedLanguage) == true) {
return // already set
}
}
updateLanguage(savedLanguage)
}
}