Android get current Locale, not default

AndroidLocale

Android Problem Overview


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()

Android Solutions


Solution 1 - Android

The default Locale is constructed statically at runtime for your application process from the system property settings, so it will represent the Locale selected on that device when the application was launched. Typically, this is fine, but it does mean that if the user changes their Locale in settings after your application process is running, the value of getDefaultLocale() probably will not be immediately updated.

If you need to trap events like this for some reason in your application, you might instead try obtaining the Locale available from the resource Configuration object, i.e.

Locale current = getResources().getConfiguration().locale;

You may find that this value is updated more quickly after a settings change if that is necessary for your application.

Solution 2 - Android

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 3 - Android

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 4 - Android

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 5 - Android

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 6 - Android

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 7 - Android

I´ve used this:

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

Solution 8 - Android

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

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

Solution 9 - Android

I used this simple code:

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCQMView Question on Stackoverflow
Solution 1 - AndroiddevunwiredView Answer on Stackoverflow
Solution 2 - AndroidMakaleleView Answer on Stackoverflow
Solution 3 - AndroidElegyDView Answer on Stackoverflow
Solution 4 - AndroidkabukoView Answer on Stackoverflow
Solution 5 - AndroidАлександр ОстапюкView Answer on Stackoverflow
Solution 6 - AndroidRakshit SoniView Answer on Stackoverflow
Solution 7 - AndroidfvaldiviaView Answer on Stackoverflow
Solution 8 - AndroidAlif HasnainView Answer on Stackoverflow
Solution 9 - Androidfarhad.kargaranView Answer on Stackoverflow