Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Android EditText, how to force writing uppercase?

People also ask

How can I type all capital letters in android?

Want to type in ALL CAPS? No problem! Just double-tap the shift key and a blue indicator will light up. You'll know you're in all caps because the letter keys will change to uppercase.

How can the user set caps to the text while reading input?

Use keyup() method to trigger the keyup event when user releases the key from keyboard. Use toLocaleUpperCase() method to transform the input to uppercase and again set it to the input element.


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

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


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"


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

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.


Use input filter

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

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