In my Android application I have different EditText
where the user can enter information. But I need to force user to write in uppercase letters.
Do you know a
Rather than worry about dealing with the keyboard, why not just accept any input, lowercase or uppercase and convert the string to uppercase?
The following code should help:
EditText edit = (EditText)findViewById(R.id.myEditText);
String input;
....
input = edit.getText();
input = input.toUpperCase(); //converts the string to uppercase
This is user-friendly since it is unnecessary for the user to know that you need the string in uppercase. Hope this helps.
In kotlin, in .kt file make changes:
edit_text.filters = edit_text.filters + InputFilter.AllCaps()
Use synthetic property for direct access of widget with id. And in XML, for your edit text add a couple of more flag as:
<EditText
android:id="@+id/edit_text_qr_code"
android:layout_width="match_parent"
android:layout_height="wrap_content"
...other attributes...
android:textAllCaps="true"
android:inputType="textCapCharacters"
/>
This will update the keyboard as upper case enabled.
Set the input type
to TYPE_CLASS_TEXT| TYPE_TEXT_FLAG_CAP_CHARACTERS
. The keyboard
should honor that.
Xamarin equivalent of ErlVolton's answer:
editText.SetFilters(editText.GetFilters().Append(new InputFilterAllCaps()).ToArray());
If you want to force user to write in uppercase letters by default in your EditText, you just need to add android:inputType="textCapCharacters"
. (User can still manually change to lowercase.)
You can used two way.
First Way:
Set android:inputType="textCapSentences"
on your EditText.
Second Way:
When user enter the number you have to used text watcher and change small to capital letter.
edittext.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
@Override
public void afterTextChanged(Editable et) {
String s=et.toString();
if(!s.equals(s.toUpperCase()))
{
s=s.toUpperCase();
edittext.setText(s);
edittext.setSelection(edittext.length()); //fix reverse texting
}
}
});