6

I am trying to return the locale in my app with this line of code:

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

The minSdkVersion is 15 and compiling with 24. But locale is deprecated, does it affect my app efficiency?

Is there any other 'not deprecated' way to retrieve the locale?

Andrii Abramov
  • 10,019
  • 9
  • 74
  • 96
NawalSalameh
  • 109
  • 1
  • 13

1 Answers1

13

If you are compiling with API 24 or above you should do this. Still it will show deprecated for lower one but you can ignore that.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    locale = getResources().getConfiguration().getLocales().get(0);
} else {
    locale = getResources().getConfiguration().locale;
}
Pushpendra
  • 2,791
  • 4
  • 26
  • 49
  • thank you, this is exactly the answer that I wanted :) – NawalSalameh Dec 27 '16 at 08:30
  • Warning: N returns a list of locales, and loading strings will try to fallback to the next locale if there are no resources available. So just looking at the first one might be meaningless. – Mihai Nita Dec 31 '16 at 04:41