How to force EditText to start text with capital letter?

Android

Android Problem Overview


I have an EditText and I need the text in it (when user types in) to be started with capital letter.

Android Solutions


Solution 1 - Android

Be careful if you add both android:capitalize="sentences" and android:inputType="text", as the latter seems to have priority over the first and the input will not be capitalized.

There's a specific inputType for automatically capitalizing the first letter:

android:inputType="textCapSentences"

See http://developer.android.com/reference/android/widget/TextView.html#attr_android:inputType

Solution 2 - Android

The options for android:capitalize are

android:inputType="none", which won't automatically capitalize anything.
 
android:inputType="sentences", Which will capitalize the first word of each sentence.
 
android:inputType="words", Which Will Capitalize The First Letter Of Every Word.
 
android:inputType="characters", WHICH WILL CAPITALIZE EVERY CHARACTER.

Apparently it has been changed to inputType instead of capitalize

Solution 3 - Android

Use

android:inputType="textPersonName|textCapWords"

as using only "textPersonName" is not enough so name's first letters would be capitalized.

Similarly with postal addresses:

android:inputType="textPostalAddress|textCapSentences"

Solution 4 - Android

Add this in your XML

 android:inputType="textCapWords"

android:inputType="textCapSentences" will work for sentences. However, I needed to capitalize every word in a full name field.

Solution 5 - Android

Try this way,

testEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS);

or android:inputType="textCapSentences" will only work If your device keyboard Auto Capitalize Setting enabled.

Solution 6 - Android

In the layout xml, add android:inputType=textCapSentences

Solution 7 - Android

You used the word "Enforce". So try this. Just pass your edittext as argument.

public static void setCapitalizeTextWatcher(final EditText editText) {
    final TextWatcher textWatcher = new TextWatcher() {

        int mStart = 0;

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            mStart = start + count;
        }

        @Override
        public void afterTextChanged(Editable s) {
             String input = s.toString();
            String capitalizedText;
            if (input.length() < 1)
                capitalizedText = input;
            else
                capitalizedText = input.substring(0, 1).toUpperCase() + input.substring(1);
            if (!capitalizedText.equals(editText.getText().toString())) {
                editText.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                        editText.setSelection(mStart);
                        editText.removeTextChangedListener(this);
                    }
                });
                editText.setText(capitalizedText);
            }
        }
    };

    editText.addTextChangedListener(textWatcher);
}

Solution 8 - Android

In the layout xml, add android:capitalize="sentences"

Solution 9 - Android

In case of password which starts from upper case it will be:

android:inputType="textPassword|textCapSentences"

Solution 10 - Android

in case you ran into the annoying case of setting android:inputType=textCapSentences then ending up with one-liner EditText, here's the solution:

    android:inputType="textCapSentences|textMultiLine"

Solution 11 - Android

Paste this in your edittext (xml):

  android:capitalize="sentences"

Solution 12 - Android

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

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
QuestionEugeneView Question on Stackoverflow
Solution 1 - AndroidMaraguesView Answer on Stackoverflow
Solution 2 - AndroidDeadlyChambersView Answer on Stackoverflow
Solution 3 - AndroidViliusKView Answer on Stackoverflow
Solution 4 - AndroidmukulgView Answer on Stackoverflow
Solution 5 - AndroidLakshmananView Answer on Stackoverflow
Solution 6 - AndroidMickey TinView Answer on Stackoverflow
Solution 7 - AndroidAzim AnsariView Answer on Stackoverflow
Solution 8 - AndroidMByDView Answer on Stackoverflow
Solution 9 - AndroidKrzysztof CieślińskiView Answer on Stackoverflow
Solution 10 - AndroidawsleimanView Answer on Stackoverflow
Solution 11 - AndroidPihuView Answer on Stackoverflow
Solution 12 - AndroidSAndroidDView Answer on Stackoverflow