How to get languages list in Android
Asked Answered
N

4

6

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?

Nullipore answered 13/1, 2011 at 17:4 Comment(1)
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
T
2

Simply use Locale.getAvailableLocales().

Teetotaler answered 14/1, 2011 at 13:11 Comment(0)
B
2

Use:

Resources.getSystem().getAssets().getLocales();
Behead answered 8/1, 2015 at 15:22 Comment(1)
You should add the essential parts of the linked article to prevent that your answer will be deleted.Archipelago
B
2
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;
}
Boscage answered 2/1, 2019 at 14:2 Comment(0)
P
0

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.

Pedicel answered 14/3, 2021 at 17:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.