18

I have already used following options to make each starting letter of a word Uppercase

 <EditText
    android:inputType="text|textCapWords"/>

While typing the user has option on the keyboard to change the case of letter i.e. the user with this option can easily type lowercase letters.

Further,I want text on my EditText to be on this format

Each Starting Letter Of A Word Must Be In Uppercase And All Other Letter Of The Word Be In Lowercase.

Meaning,when the user inputs

each StArting LeTTer of a word musT be in uppercase and all other leTTer of the word be in lowercase

, it will be automatically converted to above format.

I have tried using TextWatcher and string.split(\\s+) to get all the words and then make each and every word to follow the above format. But I always end up getting error. So if there is any solution,it would be great.I want this to work in the manner InputFilter.AllCaps.

This is my code so far

private void changeToUpperCase(String inputString) {
    if (inputString != null && inputString.trim().length() > 0) {
        // businessName.addTextChangedListener(null);
        String[] splitString = inputString.split("\\s+");
        int length = splitString.length;
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < length; i++) {
            String convertedString = splitString[i];
            stringBuffer.append(Character.toUpperCase(convertedString
                    .charAt(0)));
            stringBuffer.append(convertedString.substring(1).toLowerCase());
            stringBuffer.append(" ");
        }
        Log.i("changed String", stringBuffer.toString());
        // businessName.setText(stringBuffer.toString());
        stringBuffer.delete(0, stringBuffer.length());
        stringBuffer = null;
        // businessName.addTextChangedListener(this);
    }
}

This function I am calling from TextWatcher, afterTextChanged(Editable s)

laaptu
  • 2,963
  • 5
  • 30
  • 49

5 Answers5

25

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

The options for android:capitalize are following :

android:capitalize="none" : which won't automatically capitalize anything.

android:capitalize="sentences" : which will capitalize the first word of each sentence.

android:capitalize="words" : which will capitalize the first letter of every word.

android:capitalize="characters" : which will capitalize every character.

Update:

As android:capitalize is deprecated now need to use:

android:inputType="textCapWords"
Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138
  • @shylendar: I hope the above capitalize attribute is deprecated now.And I am using inputType="text|textCapWords" to get what you have described above. But it doesn't do what I want and I want to work the `EditText` to work in the manner,when I apply `InputFilters.AllCaps`,where it makes all the text `Uppercase`.But I want that filter to make all the text as per the pattern I asked in the question above. – laaptu Jan 17 '14 at 04:10
  • 2
    android:capitalize is deprecated . If you want to do something with your text characters you have to use android:inputType="textCapWords" for example or some of the other options – Stoycho Andreev May 11 '16 at 21:24
4

change your input type programmatically.

If you are in View layout than use this code

EditText text = new EditText(context);
text.setInputType(InputType.TYPE_TEXT_FLAG_CAP_WORDS); // which will capitalize the first letter of every word.
text.setInputType(InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS); //which will capitalize every character.
text.setInputType(InputType.TYPE_TEXT_FLAG_CAP_SENTENCES); //which will capitalize the first word of each sentence.
addView(text);

and if you are in Activity

EditText text = new EditText(this);
text.setInputType(InputType.TYPE_TEXT_FLAG_CAP_WORDS); // which will capitalize the first letter of every word.
text.setInputType(InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS); //which will capitalize every character.
text.setInputType(InputType.TYPE_TEXT_FLAG_CAP_SENTENCES); //which will capitalize the first word of each sentence.
setContentView(text);
Shubham
  • 756
  • 4
  • 13
3

To make first letter capital of every word:

android:inputType="textCapWords"

To make first letter capital of every sentence:

android:inputType="textCapSentences"

To make every letter capital:

android:inputType="textCapCharacters"
Suraj Vaishnav
  • 7,777
  • 4
  • 43
  • 46
2

Try this,

txtView.setText(WordUtils.capitalize("text view")

WordUtils.java

public class WordUtils {

    public static String capitalize(String str) {
        return capitalize(str, (char[]) null);
    }

    public static String capitalize(String str, char... delimiters) {
        int delimLen = delimiters == null ? -1 : delimiters.length;
        if (!TextUtils.isEmpty(str) && delimLen != 0) {
            char[] buffer = str.toCharArray();
            boolean capitalizeNext = true;

            for (int i = 0; i < buffer.length; ++i) {
                char ch = buffer[i];
                if (isDelimiter(ch, delimiters)) {
                    capitalizeNext = true;
                } else if (capitalizeNext) {
                    buffer[i] = Character.toTitleCase(ch);
                    capitalizeNext = false;
                }
            }

            return new String(buffer);
        } else {
            return str;
        }
    }

    private static boolean isDelimiter(char ch, char[] delimiters) {
        if (delimiters == null) {
            return Character.isWhitespace(ch);
        } else {
            char[] arr$ = delimiters;
            int len$ = delimiters.length;

            for (int i$ = 0; i$ < len$; ++i$) {
                char delimiter = arr$[i$];
                if (ch == delimiter) {
                    return true;
                }
            }

            return false;
        }
    }
}​
Jaydipsinh Zala
  • 16,558
  • 7
  • 41
  • 45
1

android:capitalize is deprecated. Use inputType instead.

Unheilig
  • 16,196
  • 193
  • 68
  • 98
Principiante
  • 131
  • 1
  • 2
  • 8