In Android EditText, how to force writing uppercase?

AndroidAndroid EdittextUppercase

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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 - Android

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 - Android

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"

Solution 4 - Android

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
      }
    }
});  

Solution 5 - Android

Use input filter

editText = (EditText) findViewById(R.id.enteredText);
editText.setFilters(new InputFilter[]{new InputFilter.AllCaps()});

Solution 6 - Android

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.

Solution 7 - Android

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

Solution 8 - Android

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!

Solution 9 - Android

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.

Solution 10 - Android

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"
                    />

Solution 11 - Android

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.

Solution 12 - Android

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

Solution 13 - Android

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

Solution 14 - Android

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"

Solution 15 - Android

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

Solution 16 - Android

try this code it will make your input into upper case

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

Solution 17 - Android

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)
}

Solution 18 - Android

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"
/>

Solution 19 - Android

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"
    />

Solution 20 - Android

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

Solution 21 - Android

Xamarin equivalent of ErlVolton's answer:

editText.SetFilters(editText.GetFilters().Append(new InputFilterAllCaps()).ToArray());

Solution 22 - Android

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.

Solution 23 - Android

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())
}

Solution 24 - Android

You can use android:inputType="textCapCharacters|textAutoComplete" in XML file

Solution 25 - Android

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"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNatacha View Question on Stackoverflow
Solution 1 - AndroidErlVoltonView Answer on Stackoverflow
Solution 2 - AndroidAoyama NanamiView Answer on Stackoverflow
Solution 3 - AndroidGabe SechanView Answer on Stackoverflow
Solution 4 - AndroidHarshidView Answer on Stackoverflow
Solution 5 - AndroidSreejesh K NairView Answer on Stackoverflow
Solution 6 - AndroidAbhishekView Answer on Stackoverflow
Solution 7 - AndroidPabelView Answer on Stackoverflow
Solution 8 - AndroidS PimentaView Answer on Stackoverflow
Solution 9 - AndroidanthonycrView Answer on Stackoverflow
Solution 10 - AndroidUdara AbeythilakeView Answer on Stackoverflow
Solution 11 - Androidvishnu bennyView Answer on Stackoverflow
Solution 12 - AndroidNivedhView Answer on Stackoverflow
Solution 13 - AndroidAnderson CarvalhoView Answer on Stackoverflow
Solution 14 - AndroidPravin SutharView Answer on Stackoverflow
Solution 15 - AndroidVladView Answer on Stackoverflow
Solution 16 - AndroidAgil anbuView Answer on Stackoverflow
Solution 17 - AndroidRoc BoronatView Answer on Stackoverflow
Solution 18 - Androidakshay shettyView Answer on Stackoverflow
Solution 19 - AndroidAshana.JackolView Answer on Stackoverflow
Solution 20 - AndroidTonyView Answer on Stackoverflow
Solution 21 - AndroidstovrozView Answer on Stackoverflow
Solution 22 - AndroidboriguenView Answer on Stackoverflow
Solution 23 - AndroidGooglianView Answer on Stackoverflow
Solution 24 - Androidimok1948View Answer on Stackoverflow
Solution 25 - AndroidKhusbooView Answer on Stackoverflow