Hello i need to get the list of the languages defined on an Android device and would like to populate a Spin开发者_JS百科ner 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().
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.
精彩评论