I'm using a searchable item with suggestion in my Android project. It is essentially an EditText
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="@string/search.hint"
android:includeInGlobalSearch="true"
android:searchSettingsDescription="@string/search.hint"
android:searchSuggestAuthority="com.xxx.android.provider.SearchSuggestionsProvider"
android:searchSuggestSelection=" ?"
android:inputType="text"
android:imeOptions="actionSearch">
</searchable>
When I start typing it shows as first letter uppercase. Why? I would like it to start lowercase. Is it possible?
I'm surprised there isn't a good answer for this yet. Or maybe it's on another question that I couldn't find.
So here's my solution.
editText.setFilters(new InputFilter[] {
new InputFilter.AllCaps() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
return String.valueOf(source).toLowerCase();
}
}
});
All text in editText
will be lowercase, no matter what.
You can modify the string however you like.
For example: you want all text to be lowercase AND no spaces allowed (let's say it's an email input field)
You can replace that return ...
like with this:
return String.valueOf(source).toLowerCase().replace(" ", "");
The same way you can allow or reject individual characters.
This example replaces all e
or E
with 3
.
return String.valueOf(source).replace("e", "3").replace("E", "3");
And so on.
I hope this helps someone.
If you want the first character to be small case by default you can use - android:capitalize="none"
. Then you will need to manually click a button to capitalize the first character. Else follow this link.
Not sure if the proposed answer works or not, but I found a bug that affected the solution I was using previously. In some keyboards (specifically Samsung ones) when the "smart text" is enabled, the chars would get duplicated.
The best solution was to create an AllLowerInputFilter class, which I basically adapted from the Android's own AllCaps implementation. It works with every keyboard I tested, with or without "smart text" enabled.
class AllLowerInputFilter : InputFilter {
override fun filter(source: CharSequence, start: Int, end: Int, dest: Spanned?, dstart: Int, dend: Int): CharSequence? {
for (i in start until end) {
if (source[i].isUpperCase()) {
val v = CharArray(end - start)
TextUtils.getChars(source, start, end, v, 0)
val s = String(v).toLowerCase()
return if (source is Spanned) {
val sp = SpannableString(s)
TextUtils.copySpansFrom(source, start, end, null, sp, 0)
sp
} else {
s
}
}
}
return null // keep original
}
}
And would be used like this:
editText.filters = arrayOf(AllLowerInputFilter())
Solution in kotlin:
editText.filters = arrayOf<InputFilter>(object : InputFilter.AllCaps() {
override fun filter(source: CharSequence?, start: Int, end: Int, dest: Spanned?, dstart: Int, dend: Int) =
source.toString().toLowerCase()
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With