Hello 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?
Thanks in advance and greetings c.
Use:
Resources.getSystem().getAssets().getLocales();
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;
}
Simply use Locale.getAvailableLocales().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With