Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use toUppercase(locale) in android 4.2?

Tags:

android

enter image description hereIm 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?

like image 343
Theresa Gamit Avatar asked Jun 25 '13 03:06

Theresa Gamit


People also ask

What is toUpperCase Locale root?

toUpperCase(Locale locale) method converts all of the characters in this String to upper case using the rules of the given Locale.

What is Android default 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.

What is root Locale?

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.

What is getLanguage () in android studio?

getLanguage(); This will return "en" or "de" or "fr" or whatever your device language is set to.


3 Answers

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);
like image 175
NameSpace Avatar answered Oct 24 '22 20:10

NameSpace


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.

like image 40
ataulm Avatar answered Oct 24 '22 19:10

ataulm


This is how I solved it:

string.toUpperCase(getResources().getConfiguration().locale));
like image 34
Droid Chris Avatar answered Oct 24 '22 18:10

Droid Chris