Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the current locale (API level 24)?

I was doing this way:

context.getResources().getConfiguration().locale 

Configuration.locale is deprecated if target is 24. So I made this change:

context.getResources().getConfiguration().getLocales().get(0) 

Now it says that it's only for minSdkVersion 24, so I cannot use it because my min target is lower.

What's the right method?

like image 433
user3290180 Avatar asked Jul 08 '16 12:07

user3290180


People also ask

How do I check my current locale?

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.

What is device locale?

Device locale - Android Tutorial Locale is the representation of a specific region.

What is use locale default?

When the Java virtual machine starts running on a computer, it creates an object called the default locale. This object affects the format of strings created from data. Depending on the default locale, a program creates different strings for the same number.


2 Answers

Check which version you're running on and fallback to the deprecated solution:

Locale locale; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {     locale = context.getResources().getConfiguration().getLocales().get(0); } else {     locale = context.getResources().getConfiguration().locale; } 
like image 59
Egor Avatar answered Sep 22 '22 12:09

Egor


You could use Locale.getDefault(), which is the Java standard way of getting the current Locale.

like image 23
Byte Welder Avatar answered Sep 20 '22 12:09

Byte Welder