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 function to do that?
This question is related to
android
android-edittext
uppercase
Just do this:
// ****** Every first letter capital in word *********
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textCapWords"
/>
//***** if all letters are capital ************
android:inputType="textCapCharacters"
A Java 1-liner of the proposed solution could be:
editText.setFilters(Lists.asList(new InputFilter.AllCaps(), editText.getFilters())
.toArray(new InputFilter[editText.getFilters().length + 1]));
Note it needs com.google.common.collect.Lists
.
I'm using Visual Studio 2015/Xamarin to build my app for both Android 5.1 and Android 6.0 (same apk installed on both).
When I specified android:inputType="textCapCharacters"
in my axml, the AllCaps keyboard appeared as expected on Android 6.0, but not Android 5.1. I added android:textAllCaps="true"
to my axml and still no AllCaps keyboard on Android 5.1. I set a filter using EditText.SetFilters(new IInputFilter[] { new InputFilterAllCaps() });
and while the soft keyboard shows lower case characters on Android 5.1, the input field is now AllCaps.
EDIT: The behavioral differences that I observed and assumed to be OS-related were actually because I had different versions of Google Keyboard on the test devices. Once I updated the devices to the latest Google Keyboard (released July 2016 as of this writing), the 'All Caps' behavior was consistent across OSes. Now, all devices show lower-case characters on the keyboard, but the input is All Caps because of SetFilters(new IInputFilter[] { new InputFilterAllCaps() });
Based on the accepted answer, this answer does the same, but in Kotlin. Just to ease copypasting :ยท)
private fun EditText.autocapitalize() {
val allCapsFilter = InputFilter.AllCaps()
setFilters(getFilters() + allCapsFilter)
}
try this code it will make your input into upper case
edittext.setFilters(new InputFilter[] {new InputFilter.AllCaps()});
It is not possible to force a capslock only via the XML. Also 3rd party libraries do not help. You could do a toUpper()
on the text on the receiving side, but there's no way to prevent it on the keyboard side
You can use XML to set the keyboard to caps lock.
Java
You can set the input_type
to TYPE_CLASS_TEXT| TYPE_TEXT_FLAG_CAP_CHARACTERS
. The keyboard
should honor that.
Kotlin
android:inputType="textCapCharacters"
You should put android:inputType="textCapCharacters"
with Edittext in xml file.
Android actually has a built-in InputFilter just for this!
edittext.setFilters(new InputFilter[] {new InputFilter.AllCaps()});
Be careful, setFilters
will reset all other attributes which were set via XML (i.e. maxLines
, inputType
,imeOptinos
...). To prevent this, add you Filter(s) to the already existing ones.
InputFilter[] editFilters = <EditText>.getFilters();
InputFilter[] newFilters = new InputFilter[editFilters.length + 1];
System.arraycopy(editFilters, 0, newFilters, 0, editFilters.length);
newFilters[editFilters.length] = <YOUR_FILTER>;
<EditText>.setFilters(newFilters);
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
}
}
});
Simply, Add below code to your EditText of your xml file.
android:digits="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
And if you want to allow both uppercase text and digits then use below code.
android:digits="ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
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.
You can add the android:textAllCaps="true"
property to your xml file in the EditText. This will enforce the softinput keyboard to appear in all caps mode. The value you enter will appear in Uppercase. However, this won't ensure that the user can only enter in UpperCase
letters. If they want, they can still fall back to the lower case letters. If you want to ensure that the output of the Edittext
is in All caps, then you have to manually convert the input String using toUpperCase()
method of String
class.
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.
Use input filter
editText = (EditText) findViewById(R.id.enteredText);
editText.setFilters(new InputFilter[]{new InputFilter.AllCaps()});
Even better... one liner in Kotlin...
// gets your previous attributes in XML, plus adds AllCaps filter
<your_edit_text>.setFilters(<your_edit_text>.getFilters() + InputFilter.AllCaps())
Done!
Simple kotlin realization
fun EditText.onlyUppercase() {
inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS
filters = arrayOf(InputFilter.AllCaps())
}
PS it seems that filters
is always empty initially
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.getText().length());
}
});
For me it worked by adding android:textAllCaps="true" and android:inputType="textCapCharacters"
<android.support.design.widget.TextInputEditText
android:layout_width="fill_parent"
android:layout_height="@dimen/edit_text_height"
android:textAllCaps="true"
android:inputType="textCapCharacters"
/>
To get capitalized keyboard when click edittext use this code in your xml,
<EditText
android:id="@+id/et"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:hint="Input your country"
android:padding="10dp"
android:inputType="textCapCharacters"
/>
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.)
Try using any one of the below code may solve your issue.
programatically:
editText.filters = editText.filters + InputFilter.AllCaps()
XML :
android:inputType="textCapCharacters" with Edittext
To get all capital, use the following in your XML:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAllCaps="true"
android:inputType="textCapCharacters"
/>
Source: Stackoverflow.com