Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

xml

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!

like image 429
Allen Gingrich Avatar asked Aug 05 '10 21:08

Allen Gingrich


People also ask

How do you capitalize the first letter in android?

If you are using an Android phone and Gboard, you can capitalize the first letter of any word with three touches. First, double-tap the word in question to highlight the word, then tap the shift button (the up arrow) on the keyboard to capitalize the first letter. Done!

How do you capitalize TextView in android?

Update. You can now use textAllCaps to force all caps. One of the answers on the linked question suggests 'android:textAllCaps="true"' This worked for me. You can do that using "textAllCaps" attribute, hence the downvote.

How do you make the first letter capital text?

To use a keyboard shortcut to change between lowercase, UPPERCASE, and Capitalize Each Word, select the text and press SHIFT + F3 until the case you want is applied.


2 Answers

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()

like image 58
Cheryl Simon Avatar answered Sep 28 '22 04:09

Cheryl Simon


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())); 
like image 32
Beto Caldas Avatar answered Sep 28 '22 06:09

Beto Caldas