2

I have a requirement that Edittext should have all words start with Capital letter. If the user writes it in a smaller case(first letter of the word), then also it should be converted it into Uppercase.

I have done it in layout as below so far :

 <EditText
             android:id="@+id/edGymName"                                       
             style="@style/LoginRegisterEditText"
             android:layout_marginTop="@dimen/size_10"
             android:layout_toLeftOf="@+id/txtStatusGymStatus"
             android:hint="@string/gym_tag"                           
          android:inputType="textPersonName|textCapWords|textNoSuggestions"
          android:maxLength="30" />

But, I don't want to allow the user to write the first letter of the word in the small letter. This is working but the user is able to write the first letter of the word in the small case. What if we forcefully do not allow it.

  • 1
    Possible duplicate of [First letter capitalization for EditText](https://stackoverflow.com/questions/4808705/first-letter-capitalization-for-edittext) – Benjith Mathew Dec 15 '17 at 07:23

5 Answers5

1

Set the input type to TYPE_CLASS_TEXT| TYPE_TEXT_FLAG_CAP_CHARACTERS.

android:inputType="textCapCharacters" for every character android:inputType="textCapSentences" for senteces android:inputType="textCapWords" for every words

Vinesh Chauhan
  • 1,288
  • 11
  • 27
  • Sirji, that i know. But, What if At the time of writing user change it to Smaller case ? I dont want let user to make it smaller at the time of writing. –  Dec 15 '17 at 08:30
1

Change input type to input type to TYPE_CLASS_TEXT| TYPE_TEXT_FLAG_CAP_CHARACTERS.

 android:inputType="text|textCapCharacters"

or from java code

 editText.setInputType(InputType.TYPE_CLASS_TEXT |InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS);
ADM
  • 20,406
  • 11
  • 52
  • 83
  • Sirji, that i know. But, What if At the time of writing user change it to Smaller case ? I dont want let user to make it smaller at the time of writing. –  Dec 15 '17 at 08:30
1

Statically (i.e. in your layout XML file): set

android:inputType="textCapSentences" on your EditText.

Programmatically: you have to include InputType.TYPE_CLASS_TEXT in the InputType of the EditText, e.g.

EditText editor = new EditText(this); 
editor.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);

User can manually change the text caps from Soft keyBoard to manage this case you can set a input filter. Android provide a AllCap filter for this.

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

Setting filter will reset some other attribute which you set in manifest. So beware of it . Like if you have set maxlenth attribute set in xml then after setting filter you need to reset it at runtime otherwise it won't work . Below is and example.

editText.setFilters(new InputFilter[] {new InputFilter.AllCaps(),new InputFilter.LengthFilter(40)});

So the best way to do it Prevent all previous filter and just add a new one.

InputFilter[] oldFilters = editText.getFilters();
    InputFilter[] newFilters = new InputFilter[oldFilters.length + 1];
    System.arraycopy(oldFilters, 0, newFilters, 0, oldFilters.length);
    newFilters[oldFilters.length] = new InputFilter.AllCaps();
    editText.setFilters(newFilters);
ADM
  • 20,406
  • 11
  • 52
  • 83
Tara
  • 692
  • 5
  • 23
  • Sirji, that i know. But, What if At the time of writing user change it to Smaller case ? I dont want let user to make it smaller at the time of writing. –  Dec 15 '17 at 08:30
1

Use this it will work.

android:inputType="textCapSentences"

In your case

<EditText
             android:id="@+id/edGymName"                                       
             style="@style/LoginRegisterEditText"
             android:layout_marginTop="@dimen/size_10"
             android:layout_toLeftOf="@+id/txtStatusGymStatus"
             android:hint="@string/gym_tag"               
             android:inputType="textCapSentences"
             android:maxLength="30" />
P.K
  • 263
  • 2
  • 9
  • Sirji, that i know. But, What if At the time of writing user change it to Smaller case ? I dont want let user to make it smaller at the time of writing. –  Dec 15 '17 at 08:31
0

This will forcefully retype your whole text/sentence from your editText and make every first letter of the word capital:

String oldText = ""; //this must be outside the method where the addTextChangedListener on the editText is set. (Preferrably outside the onCreate())

editText.addTextChangedListener(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) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            if (editable.toString().length() > 0 &&
                    !editable.toString().equals(oldText)) {
                oldText = editable.toString(); //prevent infinite loop
                editText.setText(capitalizeFirstLetterWord(editable.toString()));
                editText.setSelection(editText.getText().length()); //set the cursor to the end of the editText
            }

        }
    });

method called: (I've modified it a little bit, refer to the link)

    /**
     * reference: http://www.penguincoders.net/2015/06/program-to-capitalize-first-letter-of-each-word-in-java.html
     *
     * @param s sentence to be capitalize each first letter of each word
     * @return capitalized sentence
     */
    public static String capitalizeFirstLetterWord(String s) {
        StringBuilder cap = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            try {
                char x = s.charAt(i);
                if (x == ' ') {
                    cap.append(" ");
                    char y = s.charAt(i + 1);
                    cap.append(Character.toUpperCase(y));
                    i++;
                } else {
                    cap.append(x);
                }
            } catch (IndexOutOfBoundsException ignored) {

            }
        }

        //finally, capitalize the first letter of the sentence
        String sentence = cap.toString();
        if (sentence.length() > 0) {
            sentence = String.valueOf(sentence.charAt(0)).toUpperCase(); //capitalize first letter

            if (cap.toString().length() > 1) { //check if there's succeeding letters
                sentence += cap.toString().substring(1); //append it also
            }
        }

        return sentence;
    }
Tenten Ponce
  • 2,436
  • 1
  • 13
  • 40
  • I've tried and tested this, the only minor problem that I've encountered is the cursor of the edit text, no matter you put it, upon changing the text, the cursor will move to the end. Let me know if you've encountered any other problem aside this, (this problem could also solved if this solves your capitalization problem) – Tenten Ponce Dec 15 '17 at 08:51
  • where i can do getText() in oldText ? –  Dec 15 '17 at 09:27
  • Pardon? I can't understand your question. getText() method is only available on editText. oldText is just a String, only to avoid infinite loop. – Tenten Ponce Dec 15 '17 at 09:29
  • in my case, I've tried and tested this, the only minor problem that I've encountered is the cursor of the edit text, no matter you put it, upon changing the text, the cursor will move to the end. Let me know if you've encountered any other problem aside this, (this problem could also solved if this solves your capitalization problem) ;;; as per your comment... Pls. provide Solution. – Jaimin Modi Dec 18 '17 at 12:50
  • Actually it is not a "problem", it is just a different behavior. This method solves the question. I don't know why there's a down vote here. – Tenten Ponce Dec 19 '17 at 00:02
  • Sir, Its not satisfying the actual need. – Jaimin Modi Dec 19 '17 at 04:58
  • Did you try using it? – Tenten Ponce Dec 19 '17 at 04:59
  • yes i have tried. I am jaimin modi and user3029981 also. :) – Jaimin Modi Dec 19 '17 at 05:58