Search code examples
androidlocale

Is it possible to detect the languages selected in the Android device?


I need to know in my app which languages are selected in the device, is really easy to detect the main one:

Locale.getDefault().getLanguage()

But what I need is the list of languages, I know that this feature in Android was introduced in Lollipop(I think):

languages

So in the case of my phone (check the picture) I would like to get a list like: "en","it"

Is that possible? Thanks in advance


Solution

  • On Android versions >= N, you can get the list of Locales, but you have to provide for lower API levels as well.

    This should work, although if you want to find one specific Locale from the list, you could do it right there and avoid the copying into a List.

    final List<Locale> locales;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        final LocaleList list = getResources().getConfiguration().getLocales();
        locales = new ArrayList<>(list.size());
        for (int i = 0; i < list.size(); i++) {
            locales.add(list.get(i));
        }
    } else {
        locales = Collections.singletonList(getResources().getConfiguration().locale);
    }
    

    (you may need to change getResources() to context.getResources(), depending on where you use the code)