skip to Main Content

I’m new to android and I want to know how to capitalize the text that users enter into an EditText

3

Answers


  1. As suggested in comments you can use

    android:inputType="textCapCharacters"
    

    in your XML.

    But user can still change it to lowercase if he likes.

    If your requirement is to must bound user to UpperCase then you have to use TextWatcher in your class. When user input any text, we will change it to uppercase and set to edittext.

    You can do it as follow:

    TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    
        }
    
        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            editText.removeTextChangedListener(this);
            editText.setText(charSequence.toString().toUpperCase());
            editText.setSelection(i2);
            editText.addTextChangedListener(this);
        }
    
        @Override
        public void afterTextChanged(Editable editable) {
    
        }
    };
    

    In your onCreate of Activity

    editText.addTextChangedListener(textWatcher);
    
    Login or Signup to reply.
  2. If you use String in Kotlin you can use capitalize() extension

    Example:

    val lowerCase = "some text"
    val upperCase = lowerCase.capitalize() // Result: "Some text"
    

    In your case, you could apply that check to the text inside a TextWatcher:

    TextWatcher textWatcher = new TextWatcher() {
       @Override
       public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
    
       @Override
       public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
           editText.removeTextChangedListener(this);
           editText.setText(charSequence.toString().capitalize());
           editText.setSelection(i2);
           editText.addTextChangedListener(this);
       }
    
       @Override
       public void afterTextChanged(Editable editable) {}
    };
    

    Then:

    editText.addTextChangedListener(textWatcher);
    

    Here you can see internal implementation of capitalize method:

    public actual fun String.capitalize(): String {
        return capitalize(Locale.getDefault())
    }
    
    public fun String.capitalize(locale: Locale): String {
       if (isNotEmpty()) {
           val firstChar = this[0]
           if (firstChar.isLowerCase()) {
               return buildString {
                   val titleChar = firstChar.titlecaseChar()
                   if (titleChar != firstChar.uppercaseChar()) {
                       append(titleChar)
                   } else {
                       append([email protected](0, 1).uppercase(locale))
                   }
                   append([email protected](1))
               }
           }
       }
       return this
    }
    

    This method returns a copy of this string having its first letter titlecased using the rules of the default locale, or the original string if it’s empty or already starts with a title case letter.
    The title case of a character is usually the same as its upper case with several exceptions. The particular list of characters with the special title case form depends on the underlying platform.

    Login or Signup to reply.
  3. You just need to add This line in the EditText

        android:inputType="textCapCharacters"
    

    you will get the result that shown in fig

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search