Android get device locale

trante

Upon installation of my Android program I check for device locale:

String deviceLocale=Locale.getDefault().getLanguage();

If deviceLocale is inside my supported languages (english, french, german) I don't change locale.

But for instance say that if I don't support device's language: for example spanish.
I set current locale to English, because most of the people can understand English partly.

Locale locale = new Locale("en");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
pContext.getResources().updateConfiguration(config, null);

But after that, in other methods, when I check device's locale like this:

String deviceLocale=Locale.getDefault().getLanguage();

I get result as "English". But device's locale is Spanish.

Does getLanguage() gives current app's locale ?
How can I get device's locale ?

Edit: In app's first run, it is an option to save device locale. But if user changes device locale after I save, I can't know new locale of the device. I can only know the locale that I saved in app install.

Emanuel Moecklin

There's a much simpler and cleaner way to do this than using reflection or parsing some system property.

As you noticed once you set the default Locale in your app you don't have access to the system default Locale any more. The default Locale you set in your app is valid only during the life cycle of your app. Whatever you set in your Locale.setDefault(Locale) is gone once the process is terminated . When the app is restarted you will again be able to read the system default Locale.

So all you need to do is retrieve the system default Locale when the app starts, remember it as long as the app runs and update it should the user decide to change the language in the Android settings. Because we need that piece of information only as long as the app runs, we can simply store it in a static variable, which is accessible from anywhere in your app.

And here's how we do it:

public class MyApplication extends Application {
    
    public static String sDefSystemLanguage;
    
    @Override
    public void onCreate() {
        super.onCreate();

        sDefSystemLanguage = Locale.getDefault().getLanguage();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        sDefSystemLanguage = newConfig.locale.getLanguage();
    }

}

Using an Application (don't forget to define it in your manifest) we get the default locale when the app starts (onCreate()) and we update it when the user changes the language in the Android settings (onConfigurationChanged(Configuration)). That's all there is. Whenever you need to know what the system default language was before you used your setDefault call, sDefSystemLanguage will give you the answer.

There's one issue I spotted in your code. When you set the new Locale you do:

Configuration config = new Configuration();
config.locale = locale;
pContext.getResources().updateConfiguration(config, null);

When you do that, you overwrite all Configuration information that you might want to keep. E.g. Configuration.fontScale reflects the Accessibility settings Large Text. By settings the language and losing all other Configuration your app would not reflect the Large Text settings any more, meaning text is smaller than it should be (if the user has enabled the large text setting). The correct way to do it is:

Resources res = pContext.getResources();
Configuration config = res.getConfiguration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
    configuration.setLocale(locale);
}
else {
    configuration.locale = locale;
}
res.updateConfiguration(config, null);

So instead of creating a new Configuration object we just update the current one.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Android - change device system locale programmatically

From Java

Android get current Locale, not default

From Dev

Android get Device orientation

From Dev

Get codename of android device

From Dev

Android get Device orientation

From Dev

How to get date and time formatted using the device locale language?

From Dev

Android: How to get string in specific locale WITHOUT changing the current locale

From Dev

Android app Locale changed programatically but NotificationCompact shows message in Device language

From Dev

How to specify single image according to locale and device screen In Android

From Dev

Android get system locale different approaches

From Dev

How to get locale id in iOS/Android

From Dev

How to get the script from a Locale object on Android?

From Dev

Android Get Country Emoji Flag Using Locale

From Dev

How to get locale id in iOS/Android

From Dev

Android - Get Bluetooth UUID for this device

From Dev

Get device configuration of android mobile

From Dev

Get Owner Name of an Android Device

From Dev

Get number of installed apps on an Android device?

From Dev

Get right coordinates on touchdown on android device

From Java

How to get unique device hardware id in Android?

From Java

Android ADB commands to get the device properties

From Dev

Android : Is there anyway to get battery capacity of a device in mah?

From Dev

How to get default Typeface of Android device?

From Dev

Get access to USB mass storage device in android

From Dev

How to get Samsung Android Device Name programmatically?

From Dev

How to get phone number of the device in android activity

From Dev

How to get android device screen size?

From Dev

Ionic get installed apps on android device

From Dev

Java - Get a screenshot of connected android device

Related Related

  1. 1

    Android - change device system locale programmatically

  2. 2

    Android get current Locale, not default

  3. 3

    Android get Device orientation

  4. 4

    Get codename of android device

  5. 5

    Android get Device orientation

  6. 6

    How to get date and time formatted using the device locale language?

  7. 7

    Android: How to get string in specific locale WITHOUT changing the current locale

  8. 8

    Android app Locale changed programatically but NotificationCompact shows message in Device language

  9. 9

    How to specify single image according to locale and device screen In Android

  10. 10

    Android get system locale different approaches

  11. 11

    How to get locale id in iOS/Android

  12. 12

    How to get the script from a Locale object on Android?

  13. 13

    Android Get Country Emoji Flag Using Locale

  14. 14

    How to get locale id in iOS/Android

  15. 15

    Android - Get Bluetooth UUID for this device

  16. 16

    Get device configuration of android mobile

  17. 17

    Get Owner Name of an Android Device

  18. 18

    Get number of installed apps on an Android device?

  19. 19

    Get right coordinates on touchdown on android device

  20. 20

    How to get unique device hardware id in Android?

  21. 21

    Android ADB commands to get the device properties

  22. 22

    Android : Is there anyway to get battery capacity of a device in mah?

  23. 23

    How to get default Typeface of Android device?

  24. 24

    Get access to USB mass storage device in android

  25. 25

    How to get Samsung Android Device Name programmatically?

  26. 26

    How to get phone number of the device in android activity

  27. 27

    How to get android device screen size?

  28. 28

    Ionic get installed apps on android device

  29. 29

    Java - Get a screenshot of connected android device

HotTag

Archive