In Android EditText, how do I get to capitalize letters?

In my Android application, I have different EditText where the user can enter information. But I need to get the user to capitalize. Do you know the function for this?

+116
android android-edittext uppercase
Apr 12 '13 at 1:51
source share
22 answers

Android actually has a built-in InputFilter just for that !

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

Be careful, setFilters will reset all other attributes that were set through XML (i.e. maxLines , inputType , imeOptinos ...). To prevent this, add your Filters to 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); 
+308
Aug 29 '14 at 15:26
source share

If you want to force the user to write in default capital letters in your EditText, you just need to add android:inputType="textCapCharacters" . (The user can still manually change to lowercase.)

+108
Oct 24 '13 at 2:04 on
source share

Set input type to TYPE_CLASS_TEXT| TYPE_TEXT_FLAG_CAP_CHARACTERS TYPE_CLASS_TEXT| TYPE_TEXT_FLAG_CAP_CHARACTERS . keyboard must comply with this.

+20
Apr 12 '13 at 1:57
source share

You can use two methods.

First way:

Set android:inputType="textCapSentences" to your EditText.

The second way:

When a user enters a number, you must use a text observer and change the lowercase and uppercase letters.

 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); } } }); 
+15
Apr 12 '13 at 5:02
source share

You can add the android:textAllCaps="true" property to your XML file in EditText. This will cause the softinput keyboard to appear in all caps modes. The entered value will appear in uppercase. However, this does not guarantee that the user can enter only the letters UpperCase . If they want, they can still return to lowercase letters. If you want to make sure that the Edittext output is in all headers, you need to manually convert the input string using the toUpperCase() method of the String class.

+12
Feb 20 '15 at 10:53
source share

Instead of worrying about working 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 convenient because the user does not need to know that you need an uppercase string. Hope this helps.

+10
Apr 12 '13 at 2:01
source share

You should put android:inputType="textCapCharacters" using Edittext in the xml file.

+10
Apr 29 '15 at 8:20
source share

Use input filter

 editText = (EditText) findViewById(R.id.enteredText); editText.setFilters(new InputFilter[]{new InputFilter.AllCaps()}); 
+8
Dec 13 '17 at 6:05
source share

Just do it:

 // ****** 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" 
+6
Oct 19 '16 at 11:14
source share
 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()); } }); 
+5
Oct 12 '14 at 2:07
source share

Even better ... one liner at Kotlin ...

 // gets your previous attributes in XML, plus adds AllCaps filter <your_edit_text>.setFilters(<your_edit_text>.getFilters() + InputFilter.AllCaps()) 

Done!

+5
Nov 07 '17 at 17:23
source share

To get all the capital, use the following in your XML:

 <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:textAllCaps="true" android:inputType="textCapCharacters" /> 
+4
Jul 10 '18 at 10:25
source share

In kotlin , make changes to the .kt file:

 edit_text.filters = edit_text.filters + InputFilter.AllCaps() 

Use the synthetic property to directly access the widget with id. And in the XML for your edit text, add a couple more flags:

 <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 uppercase keyboard.

+2
Jul 29 '18 at 14:07
source share

To get the uppercase 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" /> 
+1
Jul 25 '16 at 5:58
source share

I use Visual Studio 2015 / Xamarin to build my application for both Android 5.1 and Android 6.0 (the same apk is installed on both).

When I pointed out android:inputType="textCapCharacters" in my axml, the AllCaps keyboard appeared, as expected, on Android 6.0, but not on Android 5.1. I added android:textAllCaps="true" to my axml file, but on Android 5.1 there is still no AllCaps keyboard. I set the filter using EditText.SetFilters(new IInputFilter[] { new InputFilterAllCaps() }); and although on Android 5.1 the soft keyboard displays lowercase letters, AllCaps is now used as the input field.

EDIT: The behavioral differences that I observed and presumably related to the OS were actually because I had different versions of Google Keyboard on test devices. After I updated the devices to the latest Google Keyboard (released in July 2016 at the time of this writing), the behavior of โ€œall capital lettersโ€ was the same for all operating systems. Now all devices display lowercase letters on the keyboard, but the input is SetFilters(new IInputFilter[] { new InputFilterAllCaps() }); using all uppercase SetFilters(new IInputFilter[] { new InputFilterAllCaps() }); due to SetFilters(new IInputFilter[] { new InputFilterAllCaps() });

+1
Aug 27 '16 at 15:45
source share

Based on the accepted answer, this answer does the same, but in Kotlin. Just to make copying easier:)

 private fun EditText.autocapitalize() { val allCapsFilter = InputFilter.AllCaps() setFilters(getFilters() + allCapsFilter) } 
+1
Aug 16 '17 at 16:52
source share

Xamarin equivalent ErlVolton answer:

 editText.SetFilters(editText.GetFilters().Append(new InputFilterAllCaps()).ToArray()); 
+1
Nov 01 '18 at 13:40
source share

Try using any of the codes below may solve your problem.

programmatically:

 editText.filters = editText.filters + InputFilter.AllCaps() 

XML:

 android:inputType="textCapCharacters" with Edittext 
+1
Jun 10 '19 at 9:24
source share

Simple implementation of kotlin

 fun EditText.onlyUppercase() { inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS filters = arrayOf(InputFilter.AllCaps()) } 

PS for me, filters always empty initially

0
Nov 17. '18 at 19:10
source share

The Java 1-liner proposed solution could be:

 editText.setFilters(Lists.asList(new InputFilter.AllCaps(), editText.getFilters()) .toArray(new InputFilter[editText.getFilters().length + 1])); 

Please note that this requires com.google.common.collect.Lists .

0
May 31 '19 at
source share

This worked for me 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" /> 
0
Jun 18 '19 at 5:02
source share

Just add below code to your EditText of your xml file.

 android:digits="ABCDEFGHIJKLMNOPQRSTUVWXYZ" 

And if you want to allow both heading and numbers, use the code below.

 android:digits="ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" 
-2
Mar 31 '17 at 13:17
source share



All Articles