13

I know this question has asked several times but non of the answers worked in my case.

I found some of the same sort of questions and answers in the following links , which didn't work for me at all.

LINK1

LINK2

In my layout file, I have defined my EditText as follows.

 <EditText
    android:id="@+id/test_editText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="90dp"
    android:inputType="textCapWords"
    android:ems="10" >

Also in the Activity class in the onCreate method I have added the following code.

    EditText testEditText = (EditText) findViewById(R.id.test_editText);
    testEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS); 

If someone have experienced the same issue and solved it would like to hear what I have done wrong in my case. Thanks.

Edits...

Following above explained steps I am unable to make this get it done(Auto capitalizing the first letter of each word while user typing).

According to my code, When user typing it displays first charater in all the words (including first word) in lower case.

Community
  • 1
  • 1
JibW
  • 4,538
  • 17
  • 66
  • 101
  • What happended if you remove the setInputType line ? – Alexis C. Jun 11 '13 at 10:59
  • 1
    So what is your question now man? Does it work as expected but you want to do something else? Does it not work? Under which circumstances does it not work? Does it flicker? Do you want live updates while typing? Please be more specific – avalancha Jun 11 '13 at 11:01
  • Hi ZouZou, without setInputType line it still the same. Thanks. – JibW Jun 11 '13 at 11:11
  • Hi avalancha, I just described a bit more in the "edits" in my question. Thanks. – JibW Jun 11 '13 at 11:12

10 Answers10

13
android:inputType="textCapWords"

Works for me

I used the same xml code you have in the questions:

<EditText
    android:id="@+id/test_editText"
    android:layout_width="wrap_content"
    android:layout_height="50dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="90dp"
    android:ems="10"
    android:inputType="textCapWords" >

Just check if you are overriding the inputType attribute in your Activity.

Just try without changing anything in the Activity.

Archie.bpgc
  • 23,812
  • 38
  • 150
  • 226
  • Hi Archie.bpgc, I just commented all the codes in "onCreate" method in Activity class and now there is no way to override inputtype attribyte in any way. But still when user typing, all prints in lowercase by default, if user havent selected it to be capital by the key board option. I am using an android device which runs android "2.1" OS and also tested with the emulator(Created with Nexus One with google - device definitions in virtul device manager). In your case is it automatically printing first letter of each word in Upper case while user typing? – JibW Jun 11 '13 at 11:37
  • I tried on my device(4.1.2). Yeah the keyboard starts with caps and when I click on spacebar the keyboard turns to all caps. – Archie.bpgc Jun 11 '13 at 11:41
  • Hi I think this doen't work with early OS versions. Yes I just checked, it works with OS version "4.0" for me as well now. Just check with "2.1" or something like that(Even with an emulator), it's not working... But the thing is I am looking for a solution to work with all OS versions higher than "2.1" level. Thanks. – JibW Jun 11 '13 at 11:50
  • have You got the solution ?? I am also facing the same problem with older os version but don't got the solution yet. If you got the solution then please help me. – Rizwan Mar 12 '14 at 14:36
11
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.

Lakshmanan
  • 1,671
  • 2
  • 26
  • 42
5

This is what I have done. android:inputType="textCapWords" is not working for me, either.

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) {
            //Use WordUtils.capitalizeFully if you only want the first letter of each word to be capitalized
            String capitalizedText = WordUtils.capitalize(editText.getText().toString());
            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);
}
Arst
  • 3,098
  • 1
  • 35
  • 42
  • Amazing, but getting crash when I try to empty the edittext by pressing back. Found the solution. Editing the answer – Azim Ansari Nov 19 '15 at 09:56
1

Earlier it used to be android:capitalize="words", which is now deprecated. The recommended alternative is to use android:inputType="textCapWords"

Please note that this will only work if your device keyboard Auto Capitalize Setting enabled.

To do this programatically, use the following method:

setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS);

Mahendra Liya
  • 12,912
  • 14
  • 88
  • 114
0

Have you tried android:inputType="textPersonName"

Prachi
  • 3,584
  • 2
  • 24
  • 39
0

should you try with android:inputType="textCapSentences" ?

chaitanya
  • 1,726
  • 2
  • 17
  • 13
0

Are you trying Emulator or android device? this works for me in emulator android:inputType="textCapWords"

Wasif Ali
  • 227
  • 2
  • 6
0

Simply add this code to your EditText android:capitalize="words" it must work.

Anant Shah
  • 3,744
  • 1
  • 35
  • 48
0
  1. To capitalize first letter of every word use

    android:inputType="textCapWords" Output:

    This Is Simple Text.

  2. To capitalize first letter of every sentence then use

    android:inputType="textCapSentences" Output:

    This is first statement. This is second statetment.

  3. To capitalize all letter

    android:inputType="textCapCharacters"

Output: THIS IS SIMPLE TEXT

Note: In output the bold letter used just for formatting answer. Above mentioned properties does not force these capitalization. User can enter small case letter.

Suraj Vaishnav
  • 7,777
  • 4
  • 43
  • 46
-1

TRY this on your layout...

android:inputType="textCapWords|textCapSentences"

works fine on me.. hope it works also on you...

user4144348
  • 313
  • 3
  • 8