Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get languages list in Android

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.

like image 936
Cris Avatar asked Jan 13 '11 17:01

Cris


3 Answers

Use:

Resources.getSystem().getAssets().getLocales();
like image 173
P2MS Avatar answered Oct 22 '22 14:10

P2MS


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;
}
like image 41
EpicQuest Studios Avatar answered Oct 22 '22 15:10

EpicQuest Studios


Simply use Locale.getAvailableLocales().

like image 25
gulbrandr Avatar answered Oct 22 '22 14:10

gulbrandr