Skip to content

Instantly share code, notes, and snippets.

@yongjhih
Created June 22, 2025 06:46
Show Gist options
  • Save yongjhih/adfff0aa2e4baeb0f14b1dbe4343e35a to your computer and use it in GitHub Desktop.
Save yongjhih/adfff0aa2e4baeb0f14b1dbe4343e35a to your computer and use it in GitHub Desktop.
fun EditText.maxLength(length: Int?): InputFilter.LengthFilter? {
if (length == null) {
filters = filters.filter { it !is InputFilter.LengthFilter }.toTypedArray()
return null
}
val lengthFilter = InputFilter.LengthFilter(length)
filters = filters.filter { it !is InputFilter.LengthFilter }
.toMutableList()
.apply { add(lengthFilter) }.toTypedArray()
return lengthFilter
}
var EditText.maxLength: Int?
set(value) { maxLength(value) }
get() = filters.filterIsInstance<InputFilter.LengthFilter>().firstOrNull()?.max
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment