84

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

j3App
  • 1,510
  • 1
  • 17
  • 26
Eugene
  • 59,186
  • 91
  • 226
  • 333

13 Answers13

169

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

Display Name
  • 8,022
  • 3
  • 31
  • 66
Maragues
  • 37,861
  • 14
  • 95
  • 96
  • 1
    Nice tip! I had both and it worked as expected on a Nexus One and a Galaxy Nexus. But on an HTC Sense device, the words were not capitalized. android:inputType was the trick. – BoD Sep 14 '12 at 12:45
  • 10
    Word of warning: since in my case the field was going to be a person's name, I set android:inputType="textPersonName". This did not work as expected: I had to use android:inputType="textCapWords" to get each word of the name to capitalize. – Jeff G Nov 15 '12 at 10:37
  • 2
    android:capitalize="sentences" should not use this tag bcuz it is deprecated – pioneerBhawna Nov 22 '13 at 14:06
  • What if android:inputType="textMultiLine"? – KRP May 23 '14 at 07:14
  • 14
    Then use `android:inputType="textMultiLine|textCapSentences"` – 4mnes7y Nov 18 '14 at 16:06
  • Its not working with google material **om.google.android.material.textfield.TextInputEditText** – Arbaz.in Aug 27 '19 at 09:38
24

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

DeadlyChambers
  • 5,217
  • 4
  • 44
  • 61
18

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"
ViliusK
  • 11,345
  • 4
  • 67
  • 71
8

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.

IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
Lakshmanan
  • 1,671
  • 2
  • 26
  • 42
8

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.

IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
mukulg
  • 81
  • 1
  • 3
3

In the layout xml, add android:inputType=textCapSentences

Mickey Tin
  • 3,408
  • 10
  • 42
  • 71
3

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);
}
Azim Ansari
  • 1,378
  • 11
  • 20
2

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

android:inputType="textPassword|textCapSentences"

2

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"
awsleiman
  • 1,879
  • 20
  • 14
2

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

MByD
  • 135,866
  • 28
  • 264
  • 277
1

Paste this in your edittext (xml):

  android:capitalize="sentences"
Pihu
  • 1,041
  • 11
  • 35
1
edittext.setFilters(new InputFilter[] {new InputFilter.AllCaps()});
Pang
  • 9,564
  • 146
  • 81
  • 122
SAndroidD
  • 1,745
  • 20
  • 33
  • While this code snippet may solve the question, [including an explanation](https://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations! – Box Box Box Box Jun 18 '16 at 11:27
0

Adding input type in edit text as textCapSentences which will make first letter of the sentence capital

        <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:padding="10dp"
        android:gravity="center"
        android:inputType="textCapSentences"
        android:hint="Name"/>
Sadique Khan
  • 230
  • 3
  • 9