Does this exist? I need to make a TextView which is always uppercase.
6 Answers
Try this:
<TextView
android:textAllCaps="true"
/>

- 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
-
1I 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
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) {
}
};
-
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
-
1I 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
-
1This 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
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" />
-
1the 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
-
1i 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
use
android:textAllCaps="true"
this will make your all Textview
capital.

- 2,925
- 6
- 29
- 34

- 203
- 3
- 5
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!

- 21
- 3
I found this at android developers guide:
<TextView
...
android:capitalize="characters"
...
/>

- 52,123
- 16
- 112
- 141

- 17
- 1
-
3This only applies to EditText, not TextView. From the documentation: "[...] automatically capitalize what the user types" – Guillaume Boudreau Aug 30 '12 at 19:31
-
1Real answer ?? huh .... "android:capitalize is deprecated: Use inputType instead" – Shailendra Singh Rajawat Sep 10 '13 at 15:56