Reputation: 33
I need an EditText to only allow letters and capitalize the first character.
To only allow letters, I set the property
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "
in the XML layout and it worked properly. Then, I also set the property android:inputType="textCapSentences"
to capitalize the first letter, but it didn't work.
Just to know what was going on I tried to remove the digits property and then the textCapSentences property worked fine.
So, the thing is: I can use one property or the other, but I can't get them both working at the same time. How can I solve this? May I need to solve it programmatically? Thanks.
<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "
android:inputType="textCapSentences"
android:hint="@string/et_hint" />
Upvotes: 3
Views: 1197
Reputation: 106
You can use multiple values for an attribute like this android:inputType="textCapSentences|text"
Hope this helps.
Upvotes: 0
Reputation: 4051
If you want you to capitalize each new word, you should use another inputType:
android:inputType="textCapWords"
If you want to capitalize the first letter of each new sentence, you can add .
to your digits, it will help system recognize new sentence:
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ. "
Upvotes: 0
Reputation: 649
Keep the textCapSentences property and do the letter checking programatically like this:
et_name.setFilters(new InputFilter[] { filter });
InputFilter filter = new InputFilter() {
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
for (int i = start;i < end;i++) {
if (!Character.isLetter(source.charAt(0))) {
return "";
}
}
return null;
}
};
Upvotes: 1
Reputation: 300
I don't know about using the two properties together, but if one works by itself one solution could be to use textCapSentences on your EditText and code a text filter, like so:
public static InputFilter[] myFilter = new InputFilter[] {
new InputFilter() {
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
for (int i = start; i < end; i++) {
if (!Character.isLetterOrDigit(source.charAt(i)) &&
source.charAt(i) != '@' &&
source.charAt(i) != '#') {
Log.i(TAG, "Invalid character: " + source.charAt(i));
return "";
}
}
return null;
}
}
};
This example accepts 0-9, all letters (upper and lowercase), as well as the charcters @ and #, just to give an example. If you try to enter any other character it will return "", and essentially ignore it.
Apply it when initializing the Edit Text:
EditText editText = (EditText) findViewById(R.id.my_text);
editText.setFilters(myFilter);
Upvotes: 1