In Android EditText, how to force writing uppercase?
Solution 1:
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);
Solution 2:
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.)
Solution 3:
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"