androidlocaleandroid-7.0-nougat

How to get the current locale (API level 24)?


I was doing this way:

context.getResources().getConfiguration().locale

Configuration.locale is deprecated if target is 24. So I made this change:

context.getResources().getConfiguration().getLocales().get(0)

Now it says that it's only for minSdkVersion 24, so I cannot use it because my min target is lower.

What's the right method?


Solution

  • Check which version you're running on and fallback to the deprecated solution:

    Locale locale;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        locale = context.getResources().getConfiguration().getLocales().get(0);
    } else {
        locale = context.getResources().getConfiguration().locale;
    }