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
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"
/>
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"
/>
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() });
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);
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
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.