Im using android 4.2 SDK, I have been getting a warning on this line of code:
String text0 = tagalogText.getText().toString();
String textA = text0.substring(0, 1).toUpperCase() + text0.substring(1).toLowerCase();
When I hover over it, it says:
Implicitly using the default locale is a common source of bugs: Use toUpperCase(Locale) instead.
and
Implicitly using the default locale is a common source of bugs: Use toLowerCase(Locale) instead.
I copy the code from Java, not in Java for android. Does anyone know how to remove this error? And why is it now a preferred way to use this method?
toUpperCase(Locale locale) method converts all of the characters in this String to upper case using the rules of the given Locale.
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.
The root locale is the locale whose language, country, and variant are empty ("") strings. This is regarded as the base locale of all locales, and is used as the language/country neutral locale for the locale sensitive operations.
getLanguage(); This will return "en" or "de" or "fr" or whatever your device language is set to.
Just need to clean project.
I had same trouble. I wasn't relying on the default locale, and explicitly tried Locale.US, and Locale.English, yet still got the yellow lint warning. Went away after cleaning.
string.toLowerCase(Locale.ENGLISH);
It's a lint warning, not an error. The reason is that the default locale should be used for user-facing data - if you're rendering something that the user may prefer to see in a style/format specific to their locality.
You need to decide whether it's appropriate to use the default locale:
if this is a string that will be used behind the scenes and your architecture expects this string to be uniform across all devices across the world, then you should specify a locale explicitly - usually Locale.US
if it's a user-facing string or your architecture doesn't care if it's different on other devices (i.e. it's only used internally, and isn't expected to be in a particular format other than the specified case), then you can safely ignore the lint warning.
This is how I solved it:
string.toUpperCase(getResources().getConfiguration().locale));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With