Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android get current Locale, not default

Tags:

android

locale

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

like image 414
CQM Avatar asked Jan 17 '13 22:01

CQM


People also ask

How can I get current locale language in Android?

You can use Locale. getDefault(). getLanguage(); to get the usual language code (e.g. "de", "en").

What is locale default Android?

The default locale is appropriate for tasks that involve presenting data to the user. In this case, you want to use the user's date/time formats, number formats, rules for conversion to lowercase, and so on. In this case, it's safe to use the convenience methods.

How can I get country code in Android?

setText(locale. getLanguage()); Here you use locale. getLanguage() to get the Android Locale country code of the language that is currently being used in your Android device.


2 Answers

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.

like image 66
devunwired Avatar answered Sep 20 '22 11:09

devunwired


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;         }     } 
like image 42
Makalele Avatar answered Sep 24 '22 11:09

Makalele