Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change First letter of each word to Uppercase in Textview xml

i need to change the text="font roboto regular" to Font Roboto Regular in xml itself, how to do?

<TextView
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:gravity="center"
   android:textSize="18sp"
   android:textColor="@android:color/black"
   android:fontFamily="roboto-regular"
   android:text="font roboto regular"
   android:inputType="textCapWords"
   android:capitalize="words"/>
like image 870
sree Avatar asked Feb 03 '16 12:02

sree


People also ask

Which option convert the first letter of each word into the uppercase?

Or use Word's keyboard shortcut, Shift + F3 on Windows or fn + Shift + F3 for Mac, to change selected text between lowercase, UPPERCASE or capitalizing each word.

How do you convert the first character of a string to uppercase?

The string's first character is extracted using charAt() method. Here, str. charAt(0); gives j. The toUpperCase() method converts the string to uppercase.


4 Answers

You can use this code.

String str = "font roboto regular"; String[] strArray = str.split(" "); StringBuilder builder = new StringBuilder(); for (String s : strArray) {      String cap = s.substring(0, 1).toUpperCase() + s.substring(1);      builder.append(cap + " "); } TextView textView = (TextView) findViewById(R.id.textView); textView.setText(builder.toString()); 
like image 28
Chintan Bawa Avatar answered Oct 12 '22 12:10

Chintan Bawa


If someone looking for kotlin way of doing this, then code becomes very simple and beautiful.

yourTextView.text = yourText.split(' ').joinToString(" ") { it.capitalize() } 
like image 137
Praveena Avatar answered Oct 12 '22 11:10

Praveena


Try this...

Method that convert first letter of each word in a string into an uppercase letter.

 private String capitalize(String capString){
    StringBuffer capBuffer = new StringBuffer();
    Matcher capMatcher = Pattern.compile("([a-z])([a-z]*)", Pattern.CASE_INSENSITIVE).matcher(capString);
    while (capMatcher.find()){
        capMatcher.appendReplacement(capBuffer, capMatcher.group(1).toUpperCase() + capMatcher.group(2).toLowerCase());
    }

    return capMatcher.appendTail(capBuffer).toString();
}

Usage:

String chars = capitalize("hello dream world");
//textView.setText(chars);
System.out.println("Output: "+chars);

Result:

Output: Hello Dream World
like image 35
Silambarasan Poonguti Avatar answered Oct 12 '22 12:10

Silambarasan Poonguti


KOTLIN

   val strArrayOBJ = "Your String".split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
                val builder = StringBuilder()
                for (s in strArrayOBJ) {
                    val cap = s.substring(0, 1).toUpperCase() + s.substring(1)
                    builder.append("$cap ")
                }
txt_OBJ.text=builder.toString()
like image 27
IntelliJ Amiya Avatar answered Oct 12 '22 10:10

IntelliJ Amiya