[android] First letter capitalization for EditText

I'm working on a little personal todo list app and so far everything has been working quite well. There is one little quirk I'd like to figure out. Whenever I go to add a new item, I have a Dialog with an EditText view showing inside. When I select the EditText view, the keyboard comes up to enter text, as it should. In most applications, the default seems to be that the shift key is held for the first letter... although it does not do this for my view. There has to be a simple way to fix, but I've searched the reference repeatedly and cannot find it. I'm thinking there has to be an xml attribute for the reference loaded by the Adapter, but I can't find out what it is.

This question is related to android android-edittext

The answer is


In your layout XML file :

  • set android:inputType="textCapSentences"

  • On your EditText to have first alphabet of the first word of each sentence as capital

  • Or android:inputType="textCapWords" on your EditText to have first alphabet of each word as capital


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);


Apply following line in your EditText in XML.

android:inputType="textCapSentences|textMultiLine"

It will also allow multi-line support.


if you are writing styles in styles.xml then

remove android:inputType property and add below lines

<item name="android:capitalize">words</item>

I encountered the same problem, just sharing what I found out. Might help you and others...

Try this on your layout.add the line below in your EditText.

android:inputType="textCapWords|textCapSentences"

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


If you want capital first letter in every word then use android:inputType="textCapWords"

For Better understanding

 <EditText
    android:id="@+id/edt_description"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textCapWords"/>

And if you capital word for every sentence then use it . android:inputType="textCapSentences" this line in your xml. I mean in your EditText.

For Better understanding

<EditText
    android:id="@+id/edt_description"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textCapSentences"/>
    

Try This Code, it will capitalize first character of all words.

- set addTextChangedListener for EditText view

edt_text.addTextChangedListener(watcher);

- Add TextWatcher

TextWatcher watcher = 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) {
        String input = s.toString();
        String capitalizedText;
        if (input.length() < 1)
            capitalizedText = input;
        else if (input.length() > 1 && input.contains(" ")) {
            String fstr = input.substring(0, input.lastIndexOf(" ") + 1);
            if (fstr.length() == input.length()) {
                capitalizedText = fstr;
            } else {
                String sstr = input.substring(input.lastIndexOf(" ") + 1);
                sstr = sstr.substring(0, 1).toUpperCase() + sstr.substring(1);
                capitalizedText = fstr + sstr;
            }
        } else
            capitalizedText = input.substring(0, 1).toUpperCase() + input.substring(1);

        if (!capitalizedText.equals(edt_text.getText().toString())) {
            edt_text.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) {
                    edt_text.setSelection(mStart);
                    edt_text.removeTextChangedListener(this);
                }
            });
            edt_text.setText(capitalizedText);
        }
    }
};

use this code to only First letter capitalization for EditText

MainActivity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:tag="true">
    </EditText>

</RelativeLayout>

MainActivity.java

EditText et = findViewById(R.id.et);
        et.addTextChangedListener(new TextWatcher() {
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2)
            {
                if (et.getText().toString().length() == 1 && et.getTag().toString().equals("true"))
                {
                    et.setTag("false");
                    et.setText(et.getText().toString().toUpperCase());
                    et.setSelection(et.getText().toString().length());
                }
                if(et.getText().toString().length() == 0)
                {
                    et.setTag("true");
                }
            }

            public void afterTextChanged(Editable editable) {

            }
        });

I can assure you both the answers will make first letter capital and will not make edittext single line.

If you want to do it in XMl below is the code

android:inputType="textCapWords|textCapSentences"

If want to do it in activity/fragment etc below is the code

momentTextView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS | InputType.TYPE_TEXT_FLAG_MULTI_LINE)

PS: If you are having nay other property also you can easily add it with a pipe "|" symbol, just make sure there is no space in xml between the attribute properties


Programmatically

TextView firstline = (TextView) findViewById(R.id.firstline);
firstline.setAllCaps(true);

Just use android:inputType="textCapWords" in your EditText element.

For example:

<EditText
    android:id="@+id/txtName"
    android:layout_width="0dp"
    android:layout_height="40dp"
    android:layout_weight="0.7"
    android:inputType="textCapWords"
    android:textColorHint="#aaa"
    android:hint="Name Surname"
    android:textSize="12sp" />

Refer to the following link for reference: http://developer.android.com/reference/android/widget/TextView.html#attr_android%3ainputType


For Capitalisation in EditText you can choose the below two input types:

  1. android:inputType="textCapSentences"
  2. android:inputType="textCapWords"

textCapSentences
This will let the first letter of the first word as Capital in every sentence.

textCapWords This will let the first letter of every word as Capital.

If you want both the attributes just use | sign with both the attributes

android:inputType="textCapSentences|textCapWords"

i founded and my solution : you have 2 way to resolve it in java :

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

and xml :

 <EditText
android:id="@+id/mytxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textCapWords"
android:textSize="12sp" />

To capitalize, you can do the following with edit text:

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"

But this will only make changes to keyboard and user can change the mode to write letter in small case.

So this approach is not much appreciated if you really want the data in capitalize format, add following class first:

public class CapitalizeFirstLetter {
    public static String capitaliseName(String name) {
        String collect[] = name.split(" ");
        String returnName = "";
        for (int i = 0; i < collect.length; i++) {
            collect[i] = collect[i].trim().toLowerCase();
            if (collect[i].isEmpty() == false) {
                returnName = returnName + collect[i].substring(0, 1).toUpperCase() + collect[i].substring(1) + " ";
            }
        }
        return returnName.trim();
    }
    public static String capitaliseOnlyFirstLetter(String data)
    {
        return data.substring(0,1).toUpperCase()+data.substring(1);
    }
}

And then,

Now to capitalize every word:

CapitalizeFirstLetter.capitaliseName(name);

To capitalize only first word:

CapitalizeFirstLetter.capitaliseOnlyFirstLetter(data);

Set input type in XML as well as in JAVA file like this,

In XML,

android:inputType="textMultiLine|textCapSentences"

It will also allow multiline and in JAVA file,

edittext.setRawInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);

make sure your keyboard's Auto-Capitalization setting is Enabled.


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.