2

I need to get the device Locale on android studio, but apparently the code below only works on api level 24+; How can i achieve the same result as this line of code in lower level apis?

getResources().getConfiguration().getLocales().get(0);
ADM
  • 20,406
  • 11
  • 52
  • 83
Bruno Baccho
  • 131
  • 9
  • 1
    Does this answer your question? [Android get current Locale, not default](https://stackoverflow.com/questions/14389349/android-get-current-locale-not-default) – ADM Sep 17 '20 at 04:20

2 Answers2

0

well according to google docs the command public Locale locale has deprecated in API 24 and you are allowed to use getLocales() and setLocales(android.os.LocaleList) in API levels above 24. so you should yoconfiguration.locale instead in lower APIs.

@Override
 public void applyOverrideConfiguration(Configuration overrideConfiguration) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && Build.VERSION.SDK_INT <= Build.VERSION_CODES.N_MR1) {
    // update overrideConfiguration with your locale  
    setLocale(overrideConfiguration) // you need to implement this
}
super.applyOverrideConfiguration(overrideConfiguration);
} 

also check this out, it says it all:

Set Locale programmatically

narcis dpr
  • 939
  • 2
  • 12
  • 32
0

For lower lavel api, you can use

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

Reference: https://stackoverflow.com/a/14389640/12676247

Jyotish Biswas
  • 674
  • 5
  • 11