0

neither

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

nor

android:inputType=textCapSentences / textCapWords

work for my phone? (Samsung Galaxy S2 Android 2.3.4)

and

android:capitalize="sentences"

has been deprecated

So what do I do? I read lots of topics, it seems that all of those methods work for the guys that asked the questions. I also read a topic about Word Capitalization not always being honored, because it was a suggestion rather than a directive, but this does not help me. I want the first letters capitalized...

Kaloyan Roussev
  • 14,515
  • 21
  • 98
  • 180
  • Have you tried by `android:inputType="textCapWords"`? – nKn Feb 11 '14 at 17:05
  • As I read within other SO questions, this seems to bring more problems than happiness... I've seen implementing custom methods instead, or even using `TextWatcher`s. Seems that this is one of those things that should be solved a long time ago and they still aren't... – nKn Feb 11 '14 at 17:19

1 Answers1

0

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

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