For future visitors, you can also (best IMHO) import WordUtil
from Apache
and add a lot of useful methods to you app, like capitalize
as shown here:
How to capitalize the first character of each word in a string
Please create a custom TextView and use it :
public class CustomTextView extends TextView {
public CapitalizedTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void setText(CharSequence text, BufferType type) {
if (text.length() > 0) {
text = String.valueOf(text.charAt(0)).toUpperCase() + text.subSequence(1, text.length());
}
super.setText(text, type);
}
}
For Kotlin, if you want to be sure that the format is "Aaaaaaaaa" you can use :
myString.toLowerCase(Locale.getDefault()).capitalize()
for Kotlin, just call
textview.text = string.capitalize()
The accepted answer is good, but if you are using it to get values from a textView in android, it would be good to check if the string is empty. If the string is empty it would throw an exception.
private String capitizeString(String name){
String captilizedString="";
if(!name.trim().equals("")){
captilizedString = name.substring(0,1).toUpperCase() + name.substring(1);
}
return captilizedString;
}
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()));
You can add Apache Commons Lang
in Gradle like compile 'org.apache.commons:commons-lang3:3.4'
And use WordUtils.capitalizeFully(name)
For me none of working:
Function:
private String getCapsSentences(String tagName) {
String[] splits = tagName.toLowerCase().split(" ");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < splits.length; i++) {
String eachWord = splits[i];
if (i > 0 && eachWord.length() > 0) {
sb.append(" ");
}
String cap = eachWord.substring(0, 1).toUpperCase()
+ eachWord.substring(1);
sb.append(cap);
}
return sb.toString();
}
Result:
I/P brain
O/P Brain
I/P Brain and Health
O/P Brain And Health
I/P brain And health
to O/P Brain And Health
I/P brain's Health
to O/P Brain's Health
I/P brain's Health and leg
to O/P Brain's Health And Leg
Hope this would help you.
StringBuilder sb = new StringBuilder(name);
sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
return sb.toString();
Here I have written a detailed article on the topic, as we have several options, Capitalize First Letter of String in Android
Method to Capitalize First Letter of String in Java
public static String capitalizeString(String str) {
String retStr = str;
try { // We can face index out of bound exception if the string is null
retStr = str.substring(0, 1).toUpperCase() + str.substring(1);
}catch (Exception e){}
return retStr;
}
Method to Capitalize First Letter of String in Kotlin
fun capitalizeString(str: String): String {
var retStr = str
try { // We can face index out of bound exception if the string is null
retStr = str.substring(0, 1).toUpperCase() + str.substring(1)
} catch (e: Exception) {
}
return retStr
}
Using XML Attribute
Or you can set this attribute in TextView or EditText in XML
android:inputType="textCapSentences"
Source: Stackoverflow.com