56

Does this exist? I need to make a TextView which is always uppercase.

Joren
  • 9,623
  • 19
  • 63
  • 104

6 Answers6

121

Try this:

   <TextView 
            android:textAllCaps="true"
    />
TheLD
  • 2,211
  • 4
  • 21
  • 26
  • 7
    `android:textAllCaps` only works if your minSdkVersion >= 14 (Ice Cream Sandwich) – Tim Kist Feb 14 '14 at 11:49
  • 1
    I know it's for a comment from 3+ years ago, but this only works if you're using the on-screen keyboard - if someone has a physical keyboard (bluetooth or on device) then it's ignored, it's better to use a listener – Phil A Dec 15 '15 at 11:31
34

I don't see anything like this in the TextView attributes. However, you could just make the text uppercase before setting it:

textView.setText(text.toUpperCase());

If the TextView is an EditText and you want whatever the user types to be uppercase, you could implement a TextWatcher and use the EditText addTextChangedListener to add it, and on the onTextChange method take the user input and replace it with the same text in uppercase.

editText.addTextChangedListener(upperCaseTextWatcher);

final TextWatcher upperCaseTextWatcher = new TextWatcher() {

    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    }

    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        editText.setText(editText.getText().toString().toUpperCase());
        editText.setSelection(editText.getText().toString().length());
    }

    public void afterTextChanged(Editable editable) {
    }
};
Pang
  • 9,564
  • 146
  • 81
  • 122
lander16
  • 921
  • 1
  • 9
  • 12
  • That works if `text` is a String. If you're getting the String from your xml files using `R.string.someID`, then you'll need to throw in a `Resources.getString` call (http://developer.android.com/reference/android/content/res/Resources.html#getString%28int%29) – Tyler Jul 21 '10 at 04:33
  • 1
    I was afraid I'd have to do it this way. Was hoping there'd be a simple attribute I was missing. – Joren Jul 22 '10 at 20:12
  • 4
    Won't this be creating a stackOverFlow? – desgraci Jun 28 '14 at 14:02
  • 1
    This answer is incorrect. Please refer to Lars answer: you can do it with the `textAllCaps` attribute. – zeh Jan 08 '16 at 20:05
  • Calling `editText.setText()` inside the textChanged listener will cause an infinite loop. At least it did for me without removing then re-adding the TextWatcher. – Smitty-Werben-Jager-Manjenson Feb 12 '19 at 20:48
  • Is this answer outdated? `android:textAllCaps="true" ` seems to work for me [see answers below] – O-9 Jul 23 '19 at 11:54
31

For your EditText you can use InputFilter.AllCaps as filter

editText.setFilters(new InputFilter[]{new InputFilter.AllCaps()});

See: http://d.android.com/reference/android/text/InputFilter.AllCaps.html

Also you can specify your EditText via android:inputType:

<EditText
    ...
    android:inputType="textCapCharacters" />
yanchenko
  • 56,576
  • 33
  • 147
  • 165
synergy
  • 327
  • 3
  • 3
  • 1
    the filter worked perfectly thanks a lot. however I wouldnt recommend using the inputType "textCapCharacters" since its deprecated. – Raykud Apr 17 '12 at 22:56
  • 2
    @Raykud where did you see that `textCapCharacters` is deprecated ? – sdabet Dec 04 '12 at 15:09
  • 1
    i know i am commenting very late, but once all the characters are capitalized is there something like that InputFilter.Allsmallcharacters? – Hassaan Rabbani Oct 23 '13 at 09:44
15

use

android:textAllCaps="true" 

this will make your all Textview capital.

Vinoth Krishnan
  • 2,925
  • 6
  • 29
  • 34
Dilip
  • 203
  • 3
  • 5
2

You could do it, only adding TYPE_TEXT_FLAG_CAP_CHARACTERS to InputType::

editText.setInputType(android.text.InputType.TYPE_CLASS_TEXT
                   + android.text.InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS);

I hope be helped!

0

I found this at android developers guide:

<TextView 
...
android:capitalize="characters"
...
/>
DaveShaw
  • 52,123
  • 16
  • 112
  • 141