57

I have TextView. I would like to show text in upper case mode. Is there attributed for upper case? Just I have text in strings.xml and I need use the line in several place as lower case and upper case.

Tim
  • 1,606
  • 2
  • 20
  • 32

13 Answers13

102

In the layout XML, you can set the android:textAllCaps attribute on your TextView:

<TextView android:textAllCaps="true"></TextView>
Ben Hutchison
  • 4,823
  • 4
  • 26
  • 25
77

Use String class method while setting the text to TextView as

tv.setText(strings.toUpperCase());

Have a look at this post for detail discussion.

EDIT: This was answered way back before API 14. As other answer mentioned here, textView.isAllCaps = true and its java methods were introduced from TextView. Please use those.

Adil Soomro
  • 37,609
  • 9
  • 103
  • 153
30

use this for programatically like textView.setAllCaps(false) for as it is and textView.setAllCaps(true)for Uppercase android:textAllCaps="true" for XML Layout

Rajesh
  • 2,618
  • 19
  • 25
12

Kotlin:

textView.isAllCaps = true
Metu
  • 1,033
  • 1
  • 13
  • 20
  • Since `TextView` doesn't have a getter for all caps, Kotlin doesn't make a property available to you, you still need to call `setAllCaps(true)`. – tinsukE Jul 10 '18 at 12:29
  • Weird thing to observe and only because of that I am looking over internet. I am able to use `TextView#isAllCaps` but it's giving lint error on QA's system and making him to change to `TextView#setAllCaps()`. But giving me lint warning for that – Jimit Patel Mar 19 '19 at 13:26
  • 1
    Ok got it. API version for `maxSdkVersion` less than 28 will give the lint error for `TextView#isAllCaps`. 28 and above will work smooethly – Jimit Patel Mar 19 '19 at 15:32
9

For an XML approach, you don't want to use android:capitalize because this is intended for use during text input. Instead, use textAllCaps. If your strings are declared as lower case, then it is quite simple to toggle between upper and lower case on a per-TextView basis.

calsign
  • 341
  • 3
  • 12
9

use it

<TextView android:textAllCaps="true"></TextView>
yousef
  • 1,345
  • 1
  • 12
  • 20
6

I believe there is no such attribute, but you can use

textView.setText(text.toUpperCase());

also found this, never tested by myself though

android:capitalize="characters"
slezadav
  • 6,104
  • 7
  • 40
  • 61
3

Try this.

Upper Case

textView.setText(getResources().getString(R.string.app_name).toUpperCase());

Lower Case

textView.setText(getResources().getString(R.string.app_name).toLowerCase());
Chirag
  • 56,621
  • 29
  • 151
  • 198
3

Set android:textAllCaps="true" in layout file.

Edit : If you are using kotlin use .capliatlized() method https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/capitalize.html

Gevaria Purva
  • 552
  • 5
  • 15
2

You could create a custom view derived from TextView and override the setText method to capitalize.

Sameer
  • 4,379
  • 1
  • 23
  • 23
2

You can do that easily in android xml file. you should add this line to TextView.

 <TextView 
      android:textAllCaps="true"/>

if you want to do that in code, you can do like this :

        textView.setText(strings.toUpperCase());
Whitebird
  • 218
  • 1
  • 11
1

Get a reference for your TextView and call this method on the object

    textView.setAllCaps(true);
هيثم
  • 791
  • 8
  • 11
0

Try this,

Java

textView.setAllCaps(true)

Kotlin

textView.isAllCaps = true