In my Android application I have different EditText
where the user can enter information. But I need to force user to write in uppercase letters.
Do you know a function to do that?

- 2,572
- 4
- 21
- 28

- 1,887
- 2
- 14
- 7
27 Answers
Android actually has a built-in InputFilter just for this!
edittext.setFilters(new InputFilter[] {new InputFilter.AllCaps()});
Be careful, setFilters
will reset all other attributes which were set via XML (i.e. maxLines
, inputType
,imeOptinos
...). To prevent this, add you Filter(s) to the already existing ones.
InputFilter[] editFilters = <EditText>.getFilters();
InputFilter[] newFilters = new InputFilter[editFilters.length + 1];
System.arraycopy(editFilters, 0, newFilters, 0, editFilters.length);
newFilters[editFilters.length] = <YOUR_FILTER>;
<EditText>.setFilters(newFilters);
-
this is the best answer : the lower case letters are converted automatically. Simple and efficient. Thanks – Guian Oct 08 '14 at 08:40
-
3Yes! This is the best answer! The code should be `edittext.setFilters(new InputFilter[] {new InputFilter.AllCaps()});` though. – Arthur Oct 17 '14 at 13:45
-
this should be the answer and @Arthur is right it should be `edittext.setFilters(new InputFilter[] {new InputFilter.AllCaps()});` – Axel Oct 27 '14 at 15:44
-
4OK you folks were right, I updated my answer. Thanks for helping improve this!! – ErlVolton Oct 28 '14 at 14:32
-
3Anyway to do this in xml? – Johan Jan 30 '15 at 08:36
-
this is the simplest answer i've found. – Mohammad Ersan Feb 05 '15 at 16:34
-
Instead of uppercasing the text using TextWatcher I found this as better answer. My life now is a lie. +1 For this – Glenn Sep 14 '15 at 02:32
-
4To avoid overriding the filters you set on xml (for eg. `maxLength`), try to append the `InputFilter.AllCaps()` to `editText.getFilters` array. See http://stackoverflow.com/a/18934659/3890983 – tropicalfish Aug 26 '16 at 07:48
-
@Johan see http://android--code.blogspot.com/2015/08/android-edittext-all-caps.html. This could be included in the answer too in fact. – ibizaman Sep 20 '16 at 14:57
-
1And set EditText xml property to: `android:inputType="text|textCapCharacters"` – ErickBergmann Apr 06 '17 at 17:53
-
It works @ErickBergmann. But after finding the solution I think, why not just parsing data to Capital in the save button – Hanako Sep 21 '18 at 19:19
-
38Or in Kotlin just `editText.filters = editText.filters + InputFilter.AllCaps()` – Ilia Kurtov Sep 27 '18 at 16:35
-
22Better yet: `editText.filters += InputFilter.AllCaps()` Kotlin's wonderful! <3 – Milack27 Jun 07 '19 at 16:20
If you want to force user to write in uppercase letters by default in your EditText, you just need to add android:inputType="textCapCharacters"
. (User can still manually change to lowercase.)

- 80,077
- 70
- 264
- 372

- 2,532
- 9
- 32
- 50
-
49nothing prevents the User press 'shift' and write a letter lowercased. – Fernando Leal Jul 16 '14 at 19:35
-
Agree with Fernando, while it does default the keyboard to caps, it does not require them. – pforhan Feb 04 '15 at 16:05
-
User can use shift button to enter small case letter as said by @Fernando – Ajinkya May 09 '16 at 05:13
-
this is a good solution to let force user to start with and continue typing in Capitals. Not force him, he can anyway choose to write in lowercase from Keyboard – sud007 Sep 18 '20 at 10:08
It is not possible to force a capslock only via the XML. Also 3rd party libraries do not help. You could do a toUpper()
on the text on the receiving side, but there's no way to prevent it on the keyboard side
You can use XML to set the keyboard to caps lock.
Java
You can set the input_type
to TYPE_CLASS_TEXT| TYPE_TEXT_FLAG_CAP_CHARACTERS
. The keyboard
should honor that.
Kotlin
android:inputType="textCapCharacters"

- 7,124
- 1
- 51
- 69

- 90,003
- 9
- 87
- 127
-
-
2I really don't know why this answer has so many upvotes. The solution doesn't avoid that the user disable capslock... – kuzdu Jan 13 '21 at 12:56
-
@kuzdu Since there's no API for keyboards to control that, especially not in 3rd party keyboards, there's no way to prevent that. You could do a toUpper() on the text on the receiving side, but there's no way to prevent it on the keyboard side. – Gabe Sechan Jan 13 '21 at 14:36
You can used two way.
First Way:
Set android:inputType="textCapSentences"
on your EditText.
Second Way:
When user enter the number you have to used text watcher and change small to capital letter.
edittext.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
@Override
public void afterTextChanged(Editable et) {
String s=et.toString();
if(!s.equals(s.toUpperCase()))
{
s=s.toUpperCase();
edittext.setText(s);
edittext.setSelection(edittext.length()); //fix reverse texting
}
}
});

- 27
- 5

- 5,701
- 4
- 37
- 50
-
3your solution does not work because it sets the string on a reverse order. – not 0x12 May 21 '13 at 06:16
-
-
First one is not working at all. The second one makes the text upper case but we must programmetically set the cursor to the end of the sentence. – not 0x12 May 21 '13 at 06:55
-
-
-
6For first option textCapSentences capitalises sentences, you need textCapCharacters. – Ghoti Jan 09 '14 at 12:42
-
Use input filter
editText = (EditText) findViewById(R.id.enteredText);
editText.setFilters(new InputFilter[]{new InputFilter.AllCaps()});

- 563
- 1
- 6
- 16
Even better... one liner in Kotlin...
// gets your previous attributes in XML, plus adds AllCaps filter
<your_edit_text>.setFilters(<your_edit_text>.getFilters() + InputFilter.AllCaps())
Done!

- 159
- 1
- 2
-
3You can write it using the property access syntax: `
.filters = – Makotosan Dec 18 '17 at 21:27.filters + InputFilter.AllCaps()` -
12
You can add the android:textAllCaps="true"
property to your xml file in the EditText. This will enforce the softinput keyboard to appear in all caps mode. The value you enter will appear in Uppercase. However, this won't ensure that the user can only enter in UpperCase
letters. If they want, they can still fall back to the lower case letters. If you want to ensure that the output of the Edittext
is in All caps, then you have to manually convert the input String using toUpperCase()
method of String
class.

- 920
- 13
- 23
-
@GustavoAlves, Can you just tell me what happened when you tried this ? May be I can look into your issue. :) – Abhishek Feb 25 '16 at 08:25
-
Hi @Abhishek. Setting the property didn't change anything. The EditText still accept both uppercase and lowercase characters. – Gus Costa Feb 25 '16 at 13:02
-
1Yes if you want to restrict your EditText only to accept Uppercase characters then for you, android:inputType = "textCapCharacters" would be the right option. Give it a try and then let me know. ☺️ – Abhishek Feb 25 '16 at 15:47
-
1
-
can you give me the Java code snippet where you are tying to get the String from EditText ?? – Abhishek Nov 25 '16 at 11:23
-
3`android:textAllCaps="true"` is only a property of TextView (https://stackoverflow.com/a/28803492/5268730). Use `android:inputType="textCapCharacters"` instead. – woodii Apr 13 '18 at 12:14
You should put android:inputType="textCapCharacters"
with Edittext in xml file.
-
10AoyamaNanami wrote exactly the same answer. Please focus on more current questions, this one is from 2013. :) – ByteHamster Apr 29 '15 at 08:37
-
2
Rather than worry about dealing with the keyboard, why not just accept any input, lowercase or uppercase and convert the string to uppercase?
The following code should help:
EditText edit = (EditText)findViewById(R.id.myEditText);
String input;
....
input = edit.getText();
input = input.toUpperCase(); //converts the string to uppercase
This is user-friendly since it is unnecessary for the user to know that you need the string in uppercase. Hope this helps.

- 4,146
- 1
- 28
- 35
For me it worked by adding android:textAllCaps="true" and android:inputType="textCapCharacters"
<android.support.design.widget.TextInputEditText
android:layout_width="fill_parent"
android:layout_height="@dimen/edit_text_height"
android:textAllCaps="true"
android:inputType="textCapCharacters"
/>

- 1,215
- 1
- 20
- 31
In kotlin, in .kt file make changes:
edit_text.filters = edit_text.filters + InputFilter.AllCaps()
Use synthetic property for direct access of widget with id. And in XML, for your edit text add a couple of more flag as:
<EditText
android:id="@+id/edit_text_qr_code"
android:layout_width="match_parent"
android:layout_height="wrap_content"
...other attributes...
android:textAllCaps="true"
android:inputType="textCapCharacters"
/>
This will update the keyboard as upper case enabled.

- 998
- 1
- 11
- 15
Try using any one of the below code may solve your issue.
programatically:
editText.filters = editText.filters + InputFilter.AllCaps()
XML :
android:inputType="textCapCharacters" with Edittext

- 971
- 1
- 8
- 19
edittext.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
@Override
public void afterTextChanged(Editable et) {
String s=et.toString();
if(!s.equals(s.toUpperCase()))
{
s=s.toUpperCase();
edittext.setText(s);
}
editText.setSelection(editText.getText().length());
}
});

- 25,539
- 11
- 55
- 67

- 89
- 1
- 1
Just do this:
// ****** Every first letter capital in word *********
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textCapWords"
/>
//***** if all letters are capital ************
android:inputType="textCapCharacters"

- 9,564
- 146
- 81
- 122

- 1,403
- 1
- 14
- 25
Simple kotlin realization
fun EditText.onlyUppercase() {
inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS
filters = arrayOf(InputFilter.AllCaps())
}
PS it seems that filters
is always empty initially

- 7,997
- 3
- 56
- 43
try this code it will make your input into upper case
edittext.setFilters(new InputFilter[] {new InputFilter.AllCaps()});

- 71
- 1
- 4
Based on the accepted answer, this answer does the same, but in Kotlin. Just to ease copypasting :·)
private fun EditText.autocapitalize() {
val allCapsFilter = InputFilter.AllCaps()
setFilters(getFilters() + allCapsFilter)
}

- 11,395
- 5
- 47
- 59
To get all capital, use the following in your XML:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAllCaps="true"
android:inputType="textCapCharacters"
/>

- 194
- 3
- 10
-
2have you tried this solution? this results in crash of application when user start to edit the content of editext – surhidamatya Jan 14 '19 at 06:15
To get capitalized keyboard when click edittext use this code in your xml,
<EditText
android:id="@+id/et"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:hint="Input your country"
android:padding="10dp"
android:inputType="textCapCharacters"
/>

- 3,064
- 28
- 22
I'm using Visual Studio 2015/Xamarin to build my app for both Android 5.1 and Android 6.0 (same apk installed on both).
When I specified android:inputType="textCapCharacters"
in my axml, the AllCaps keyboard appeared as expected on Android 6.0, but not Android 5.1. I added android:textAllCaps="true"
to my axml and still no AllCaps keyboard on Android 5.1. I set a filter using EditText.SetFilters(new IInputFilter[] { new InputFilterAllCaps() });
and while the soft keyboard shows lower case characters on Android 5.1, the input field is now AllCaps.
EDIT: The behavioral differences that I observed and assumed to be OS-related were actually because I had different versions of Google Keyboard on the test devices. Once I updated the devices to the latest Google Keyboard (released July 2016 as of this writing), the 'All Caps' behavior was consistent across OSes. Now, all devices show lower-case characters on the keyboard, but the input is All Caps because of SetFilters(new IInputFilter[] { new InputFilterAllCaps() });

- 8,758
- 3
- 49
- 61

- 363
- 2
- 8
-
1Hi Tony, it seems you have created 2 accounts; http://stackoverflow.com/users/6764633/tony-celia and http://stackoverflow.com/users/6771440/deeveedee. If you want to merge them, see http://stackoverflow.com/help/merging-accounts. – Matt Sep 17 '16 at 09:57
-
Thanks, Matt. Chalk this one up to beginner learning/mistakes. Didn't remember that I had created another account until you pointed it out. fortunately, the account merge function was simple. Great forum! – Tony Mar 06 '17 at 15:30
Xamarin equivalent of ErlVolton's answer:
editText.SetFilters(editText.GetFilters().Append(new InputFilterAllCaps()).ToArray());

- 6,835
- 2
- 48
- 59
A Java 1-liner of the proposed solution could be:
editText.setFilters(Lists.asList(new InputFilter.AllCaps(), editText.getFilters())
.toArray(new InputFilter[editText.getFilters().length + 1]));
Note it needs com.google.common.collect.Lists
.

- 21
- 3
2021: Answer
This is the only latest answer if you want to get the EditText
from EditTextPreference
.
In order to change the EditText
value or attributes, you need to set setOnBindEditTextListener
callback as per the new AndroidX Kotlin.
findPreference<EditTextPreference>("key")?.setOnBindEditTextListener {
it.filters = arrayOf<InputFilter>(InputFilter.AllCaps())
}

- 6,077
- 3
- 38
- 44
On your XML side where you use an editText, this code below will force the input-user-text to make them all capital without Java code but XML:
android:inputType="textAutoCorrect|none|numberSigned|textMultiLine|textNoSuggestions|number|datetime|textWebEmailAddress|textPersonName|textCapSentences|textPassword|textAutoComplete|textImeMultiLine|numberDecimal"
These can make the text input area all in capital by force with no doubt. If you want to make not AllCaps, to turn off, don't write "numberSigned" in your XML code above.

- 467
- 7
- 22
For Jetpack Compose TextField, it can be achieved in these ways
Setting a keyboard option
keyboardOptions = KeyboardOptions(capitalization = KeyboardCapitalization.Characters)
Or applying a visual transformation
class CapsCharacterVisualTransformation: VisualTransformation {
override fun filter(text: AnnotatedString): TransformedText {
return TransformedText(AnnotatedString(text.text.uppercase()), OffsetMapping.Identity)
}
}
// usage
visualTransformation = CapsCharacterVisualTransformation()

- 1,978
- 23
- 33
Simply, Add below code to your EditText of your xml file.
android:digits="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
And if you want to allow both uppercase text and digits then use below code.
android:digits="ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
-
2That works for sure, but it's not a very nice user experience. I just tried it out and as the keyboard doesn't autocapitalise (on N at least), lowercase typed characters are just ignored with no feedback to the user, making the UI seem broken. – MattMatt Apr 13 '17 at 22:16
-
completely agree with @MattMatt. If we do it the user will not be able to type small case letters. The user should be able to type whatever case he/she wants and the rest should auto uppercase the words – Mir Adnan Jun 18 '17 at 23:31
-
I don't understand the downvotes. This is not the solution but another strategy, and it could be supplementary. In my case the user is setting a new password and the characters are not shown by default, if I let him write lowercase and convert to uppercase behind user's back it could lead to errors if he doesn't check what is writing. I think setting `android:inputType="textCapCharacters"` together with `android:digits="ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"` is the best approach for users still trying to write lowercase. – marcRDZ Aug 29 '23 at 07:05