I need to get the list of the languages defined on an Android device and would like to populate a Spinner with language code and language name. How can I do that?
How to get languages list in Android
Asked Answered
What do you mean by 'defined on an Android device?' Do you just mean the possible Locales? (such as en-rUS, fr-rFR, etc.) –
Vantage
Use:
Resources.getSystem().getAssets().getLocales();
You should add the essential parts of the linked article to prevent that your answer will be deleted. –
Archipelago
public static List<String> getLanguages() {
String[] locales = Resources.getSystem().getAssets().getLocales();
List<String> list = new ArrayList<>();
for (Locale locale : Locale.getAvailableLocales()) {
if (locale.getLanguage().length() == 2) {
if (!isLanguageInList(list, locale)) {
list.add(locale.getDisplayLanguage());
}
}
}
Collections.sort(list);
return list;
}
private static boolean isLanguageInList(List<String> list, Locale locale) {
if (list == null) {
return false;
}
for (String item: list) {
if (item.equalsIgnoreCase(locale.getDisplayLanguage())){
return true;
}
}
return false;
}
I've faced same issue. I've figured out how get locales defined in
phone settings -> system -> languages and input
kotlin
val locales = Resources.getSystem().configuration.locales
this works since API 24.
© 2022 - 2024 — McMap. All rights reserved.