How to capitalize the first letter of text in a TextView in an Android Application

I'm not referring to textInput, either. I mean that once you have static text in a TextView (populated from a Database call to user inputted data (that may not be Capitalized)), how can I make sure they are capitalized?

Thanks!


Solution 1:

I should be able to accomplish this through standard java string manipulation, nothing Android or TextView specific.

Something like:

String upperString = myString.substring(0, 1).toUpperCase() + myString.substring(1).toLowerCase();

Although there are probably a million ways to accomplish this. See String documentation.

EDITED I added the .toLowerCase()

Solution 2:

Following does not apply to TextView, but works with EditText; even then, it applies to the text entered from the keyboard, not the text loaded with setText(). To be more specific, it turns the Caps on in the keyboard, and the user can override this at her will.

android:inputType="textCapSentences"

or

TV.sname.setInputType(InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);

This will CAP the first letter.

or

compile 'org.apache.commons:commons-lang3:3.4' //in build.gradle module(app)

tv.setText(StringUtils.capitalize(myString.toLowerCase().trim()));

Solution 3:

for Kotlin, just call

textview.text = string.capitalize()

Solution 4:

StringBuilder sb = new StringBuilder(name);
sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));  
return sb.toString();

Solution 5:

You can add Apache Commons Lang in Gradle like compile 'org.apache.commons:commons-lang3:3.4'

And use WordUtils.capitalizeFully(name)