Reputation: 856
I want forcefully add first letter capital in EditText
. So i can search about i get lots of solution but i Notice that user can shift and enter lower characters.
I tried below code :
android:inputType="textCapSentences|textCapWords"
Also I tried pragmatically :
EditText editor = new EditText(this);
editor.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);
Any other solution. If user shift and enter lower characters it's automatically converted in to Upper characters.
Upvotes: 3
Views: 4784
Reputation: 320
You can use android:inputType="textCapWords"
to capitalize every word and android:inputType="textCapSentences"
for capitalising every sentence
Upvotes: 1
Reputation: 1127
Hello Guys Below code is worked for me.
val toUpperCaseFilter =
InputFilter { source, start, end, dest, dstart, dend ->
val stringBuilder = StringBuilder()
for (i in start until end) {
var character = source[i]
if(i==0 || source[i-1].equals(' ',true)) {
character = Character.toUpperCase(character!!) // THIS IS UPPER CASING
}
stringBuilder.append(character)
}
stringBuilder.toString()
}
binding.edtUsername.setFilters(arrayOf(toUpperCaseFilter))
This will work in every device and every keyboard.
Upvotes: 1
Reputation: 11477
Simple solution - Cannot restrict user to type first character capital so
When the user is done writing get the string value from edittext
then change the first letter to capital and use that string.
ie
On Button click
or so get the string
value from edittext
String content = edtEditText.getText().toString();
content.substring(0, 1).toUpperCase() + str.substring(1);
Upvotes: 1
Reputation: 4023
Try a text watcher like following
EditText et = (EditText)findViewById(R.id.editText);
et.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(count ==1){
Character.toUpperCase(s.charAt(0));
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
@Override
public void afterTextChanged(Editable s) {}
});
Upvotes: 0
Reputation: 1183
Try InputFilter
here is working snipped. modify code acording your need.
EditText editText = (EditText) findViewById(R.id.editTextId);
editText..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: 2484
You just need to set android:capitalize="sentences"
in your XML file, No need to set other "textCapWords" in android:inputType
, as the latter seems to have priority over the first and the input will not be capitalized.
Upvotes: 0