I'm new to android and I want to know how to capitalize the text that users enter into an EditText
CodePudding user response:
As suggested in comments you can use
android:inputType="textCapCharacters"
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:
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) {
}
};
In your onCreate of Activity
editText.addTextChangedListener(textWatcher);