But user can still change it to lowercase if he likes.
If your requirement is to must bound user to UpperCase then you have to use TextWatcher in your class. When user input any text, we will change it to uppercase and set to edittext.
You can do it as follow:
TextWatcher textWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
editText.removeTextChangedListener(this);
editText.setText(charSequence.toString().toUpperCase());
editText.setSelection(i2);
editText.addTextChangedListener(this);
}
@Override
public void afterTextChanged(Editable editable) {
}
};
If you use String in Kotlin you can use capitalize() extension
Example:
val lowerCase = "some text"
val upperCase = lowerCase.capitalize() // Result: "Some text"
In your case, you could apply that check to the text inside a TextWatcher:
TextWatcher textWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
editText.removeTextChangedListener(this);
editText.setText(charSequence.toString().capitalize());
editText.setSelection(i2);
editText.addTextChangedListener(this);
}
@Override
public void afterTextChanged(Editable editable) {}
};
Then:
editText.addTextChangedListener(textWatcher);
Here you can see internal implementation of capitalize method:
public actual fun String.capitalize(): String {
return capitalize(Locale.getDefault())
}
public fun String.capitalize(locale: Locale): String {
if (isNotEmpty()) {
val firstChar = this[0]
if (firstChar.isLowerCase()) {
return buildString {
val titleChar = firstChar.titlecaseChar()
if (titleChar != firstChar.uppercaseChar()) {
append(titleChar)
} else {
append([email protected](0, 1).uppercase(locale))
}
append([email protected](1))
}
}
}
return this
}
This method returns a copy of this string having its first letter titlecased using the rules of the default locale, or the original string if it’s empty or already starts with a title case letter.
The title case of a character is usually the same as its upper case with several exceptions. The particular list of characters with the special title case form depends on the underlying platform.
3
Answers
As suggested in comments you can use
in your XML.
But user can still change it to lowercase if he likes.
If your requirement is to must bound user to UpperCase then you have to use TextWatcher in your class. When user input any text, we will change it to uppercase and set to edittext.
You can do it as follow:
In your onCreate of Activity
If you use String in Kotlin you can use capitalize() extension
Example:
In your case, you could apply that check to the text inside a TextWatcher:
Then:
Here you can see internal implementation of capitalize method:
This method returns a copy of this string having its first letter titlecased using the rules of the default locale, or the original string if it’s empty or already starts with a title case letter.
The title case of a character is usually the same as its upper case with several exceptions. The particular list of characters with the special title case form depends on the underlying platform.
You just need to add This line in the EditText
you will get the result that shown in fig