Skip to content Skip to sidebar Skip to footer

How To Lowercase The Text In The EditText Of The Searchable Item?

I'm using a searchable item with suggestion in my Android project. It is essentially an EditText

Solution 1:

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.


Solution 2:

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.


Solution 3:

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 4:

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()
        })

Solution 5:

There are a variety of inputType options, but none specifically to keep text lowercase.

You may want to look through the complete list of them here: http://developer.android.com/reference/android/widget/TextView.html#attr_android:inputType

I would expect "textUri" would do what you want.

You can always just use the String.toLowerCase() function on the text, after it has been entered.

Otherwise, you could add add a TextWatcher to your EditText, and validate each input event, based on your specific criteria.


Post a Comment for "How To Lowercase The Text In The EditText Of The Searchable Item?"