First letter capitalization for EditText

AndroidAndroid Edittext

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

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

>Can be combined with text and its variations to request capitalization of the first character of every sentence.

- Google Docs

Solution 2 - Android

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

Solution 3 - Android

Apply following line in your EditText in XML.

android:inputType="textCapSentences|textMultiLine"

It will also allow multi-line support.

Solution 4 - Android

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.

Solution 5 - Android

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...

Solution 6 - Android

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

Solution 7 - Android

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"/>
    

Solution 8 - Android

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"

Solution 9 - Android

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

Solution 10 - Android

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

Solution 11 - Android

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

Solution 12 - Android

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.

Solution 13 - Android

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

            }
        });

Solution 14 - Android

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" />

Solution 15 - Android

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

Solution 16 - Android

Programmatically

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

Solution 17 - Android

if you are writing styles in styles.xml then

remove android:inputType property and add below lines

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMaximusView Question on Stackoverflow
Solution 1 - AndroidMcStretchView Answer on Stackoverflow
Solution 2 - AndroidMangi MorobeView Answer on Stackoverflow
Solution 3 - AndroidShivangView Answer on Stackoverflow
Solution 4 - AndroidLakshmananView Answer on Stackoverflow
Solution 5 - Androiduser4144348View Answer on Stackoverflow
Solution 6 - AndroidSuraj VaishnavView Answer on Stackoverflow
Solution 7 - AndroidJewel RanaView Answer on Stackoverflow
Solution 8 - AndroidAnand TripathiView Answer on Stackoverflow
Solution 9 - AndroidTushar SrivastavaView Answer on Stackoverflow
Solution 10 - AndroidHimmat GillView Answer on Stackoverflow
Solution 11 - AndroidDharmesh PatelView Answer on Stackoverflow
Solution 12 - AndroidSiddharth ShethView Answer on Stackoverflow
Solution 13 - Androidmilan pithadiaView Answer on Stackoverflow
Solution 14 - Androiduser8427383View Answer on Stackoverflow
Solution 15 - AndroidMahendra LiyaView Answer on Stackoverflow
Solution 16 - AndroidRajesh TiwariView Answer on Stackoverflow
Solution 17 - Androidsaigopi.meView Answer on Stackoverflow