How to force EditText to start text with capital letter?
AndroidAndroid Problem Overview
I have an EditText
and I need the text in it (when user types in) to be started with capital letter.
Android Solutions
Solution 1 - Android
Be careful if you add both android:capitalize="sentences"
and android:inputType="text"
, as the latter seems to have priority over the first and the input will not be capitalized.
There's a specific inputType
for automatically capitalizing the first letter:
android:inputType="textCapSentences"
See http://developer.android.com/reference/android/widget/TextView.html#attr_android:inputType
Solution 2 - Android
The options for android:capitalize are
android:inputType="none", which won't automatically capitalize anything.
android:inputType="sentences", Which will capitalize the first word of each sentence.
android:inputType="words", Which Will Capitalize The First Letter Of Every Word.
android:inputType="characters", WHICH WILL CAPITALIZE EVERY CHARACTER.
Apparently it has been changed to inputType
instead of capitalize
Solution 3 - Android
Use
android:inputType="textPersonName|textCapWords"
as using only "textPersonName"
is not enough so name's first letters would be capitalized.
Similarly with postal addresses:
android:inputType="textPostalAddress|textCapSentences"
Solution 4 - Android
Add this in your XML
android:inputType="textCapWords"
android:inputType="textCapSentences"
will work for sentences. However, I needed to capitalize every word in a full name field.
Solution 5 - Android
Try this way,
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 6 - Android
In the layout xml, add android:inputType=textCapSentences
Solution 7 - Android
You used the word "Enforce". So try this. Just pass your edittext as argument.
public static void setCapitalizeTextWatcher(final EditText editText) {
final TextWatcher textWatcher = 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
capitalizedText = input.substring(0, 1).toUpperCase() + input.substring(1);
if (!capitalizedText.equals(editText.getText().toString())) {
editText.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) {
editText.setSelection(mStart);
editText.removeTextChangedListener(this);
}
});
editText.setText(capitalizedText);
}
}
};
editText.addTextChangedListener(textWatcher);
}
Solution 8 - Android
In the layout xml, add android:capitalize="sentences"
Solution 9 - Android
In case of password which starts from upper case it will be:
android:inputType="textPassword|textCapSentences"
Solution 10 - Android
in case you ran into the annoying case of setting android:inputType=textCapSentences then ending up with one-liner EditText, here's the solution:
android:inputType="textCapSentences|textMultiLine"
Solution 11 - Android
Paste this in your edittext (xml):
android:capitalize="sentences"
Solution 12 - Android
edittext.setFilters(new InputFilter[] {new InputFilter.AllCaps()});