Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android EditText/TextView how to make each word start with uppercase and all remaining characters of words to be lowercase

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)

like image 766
laaptu Avatar asked Jan 16 '14 06:01

laaptu


5 Answers

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"
like image 133
Shailendra Madda Avatar answered Oct 22 '22 19:10

Shailendra Madda


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);
like image 36
Shubham Avatar answered Oct 22 '22 18:10

Shubham


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;
        }
    }
}​
like image 42
Jaydipsinh Zala Avatar answered Oct 22 '22 19:10

Jaydipsinh Zala


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"
like image 3
Suraj Vaishnav Avatar answered Oct 22 '22 19:10

Suraj Vaishnav


android:capitalize is deprecated. Use inputType instead.

like image 1
Principiante Avatar answered Oct 22 '22 18:10

Principiante