12

I have a TextView. I'm trying to capitalize the first letter in every word.

Here's the TextView:

 <TextView 
            android:text="TextView" 
            android:id="@+id/textView1" 
            android:layout_height="wrap_content" 
            android:layout_width="wrap_content" 
            android:layout_alignParentRight="true" 
            android:textSize="30dip" 
            android:textStyle="bold" 
            android:layout_marginRight="5dip" 
            android:ellipsize="end"
            android:capitalize="words">
        </TextView>

Here's how I'm adding the text:

TextView titleView = (TextView) findViewById(R.id.textView1);
    titleView.setText( section.replace("_", " ") );

Can I not add text dynamically and expect it to capitalize the words? Is another trait interfering with android:capitalize? Is android:capitalize broken?

Thanks for your replies.

emachine
  • 995
  • 4
  • 16
  • 25

7 Answers7

16

If you're targeting API Level 14 and above, you should use

android:textAllCaps="true"

Otherwise, you'll have to implement this behavior yourself.

Nir Pear
  • 291
  • 3
  • 7
15

capitalize is basically just a KeyListener that you can set in XML, so it only applies to text input by the user. As the documentation states (emphasis mine):

If set, specifies that this TextView has a textual input method and should automatically capitalize what the user types.

There is a related question on how to capitalize the first letter of every word in Java which has some helpful answers.

Community
  • 1
  • 1
eldarerathis
  • 32,541
  • 9
  • 86
  • 93
6

android:capitalize is now deprecated.

Instead of using android:capitalize="words", you should consider using android:inputType="textCapWords".

Depending on your needs, you can also use multiple values, such as android:inputType="textCapWords|textPersonName".

ban-geoengineering
  • 15,533
  • 18
  • 140
  • 225
1

This is a late answer but I think might help someone...

If you are comfortable with capitilizing the dynamic text in the java code then you can use:

textView.setText(text.toUpperCase()); 
Ahmed
  • 3,997
  • 8
  • 37
  • 70
0

@Shine's answer is correct I don't know why it was down voted. android:capitalize was deprecated in API 3. Unfortunately the TextView docs fail to indicate this, the proof is burried in R.attr:

R.attr

Android studio also fails to inform you that this attribute is deprecated. Another 30 mins I'll never get back, thanks google!

user3259330
  • 428
  • 6
  • 9
0

In kotlin gives

  private val replacementTransformationMethod: ReplacementTransformationMethod =
    object : ReplacementTransformationMethod() {
        override fun getOriginal(): CharArray {
            return charArrayOf(
                'a',
                'b',
                'c',
                'd',
                'e',
                'f',
                'g',
                'h',
                'i',
                'j',
                'k',
                'l',
                'm',
                'n',
                'o',
                'p',
                'q',
                'r',
                's',
                't',
                'u',
                'v',
                'w',
                'x',
                'y',
                'z'
            )
        }

        override fun getReplacement(): CharArray {
            return charArrayOf(
                'A',
                'B',
                'C',
                'D',
                'E',
                'F',
                'G',
                'H',
                'I',
                'J',
                'K',
                'L',
                'M',
                'N',
                'O',
                'P',
                'Q',
                'R',
                'S',
                'T',
                'U',
                'V',
                'W',
                'X',
                'Y',
                'Z'
            )
        }
    }

then in the block override fun onActivityCreated(savedInstanceState: Bundle?)

set

this.textUserCode.transformationMethod = replacementTransformationMethod
Jerry
  • 231
  • 1
  • 3
  • 10
-1

android:capitalize is deprecated on ICS, so I guess it should be better to do it in code (i.e. with String.toUpperCase())

Another try could be

 android:inputType="textCapWords"

but I guess it would require an EditText to work.

I don't know if this is the reason for your code, what version are you targeting?

Shine
  • 3,603
  • 1
  • 30
  • 54