'Android get current Locale, not default

How do I get the user's current Locale in Android?

I can get the default one, but this may not be the current one correct?

Basically I want the two letter language code from the current locale. Not the default one. There is no Locale.current()



Solution 1:[1]

Android N (Api level 24) update (no warnings):

   Locale getCurrentLocale(Context context){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
            return context.getResources().getConfiguration().getLocales().get(0);
        } else{
            //noinspection deprecation
            return context.getResources().getConfiguration().locale;
        }
    }

Solution 2:[2]

If you are using the Android Support Library you can use ConfigurationCompat instead of @Makalele's method to get rid of deprecation warnings:

Locale current = ConfigurationCompat.getLocales(getResources().getConfiguration()).get(0);

or in Kotlin:

val currentLocale = ConfigurationCompat.getLocales(resources.configuration)[0]

Solution 3:[3]

From getDefault's documentation:

Returns the user's preferred locale. This may have been overridden for this process with setDefault(Locale).

Also from the Locale docs:

The default locale is appropriate for tasks that involve presenting data to the user.

Seems like you should just use it.

Solution 4:[4]

All answers above - do not work. So I will put here a function that works on 4 and 9 android

private String getCurrentLanguage(){
   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
      return LocaleList.getDefault().get(0).getLanguage();
   } else{
      return Locale.getDefault().getLanguage();
   }
}

Solution 5:[5]

As per official documentation ConfigurationCompat is deprecated in support libraries

You can consider using

LocaleListCompat.getDefault()[0].toLanguageTag() 0th position will be user preferred locale

To get Default locale at 0th position would be LocaleListCompat.getAdjustedDefault()

Solution 6:[6]

I´ve used this:

String currentLanguage = Locale.getDefault().getDisplayLanguage();
if (currentLanguage.toLowerCase().contains("en")) {
   //do something
}

Solution 7:[7]

As for now, we can use ConfigurationCompat class to avoid warnings and unnecessary boilerplates.

Locale current = ConfigurationCompat.getLocales(getResources().getConfiguration()).get(0);

Solution 8:[8]

I used this simple code:

if(getResources().getConfiguration().locale.getLanguage().equalsIgnoreCase("en"))
{
   //do something
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ericn
Solution 2
Solution 3 kabuko
Solution 4 ????????? ???????
Solution 5 Community
Solution 6 fvaldivia
Solution 7 Alif Hasnain
Solution 8 farhad.kargaran