Skip to content

Instantly share code, notes, and snippets.

@sgallego
Last active December 13, 2019 21:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sgallego/1e63d1356f2815d1a12fd2bfa095d171 to your computer and use it in GitHub Desktop.
Save sgallego/1e63d1356f2815d1a12fd2bfa095d171 to your computer and use it in GitHub Desktop.
InputFilter to enter years from 1900 onwards
val yearInputFilter: InputFilter = object : InputFilter {
override fun filter(source: CharSequence?, start: Int, end: Int, dest: Spanned?, dStart: Int, dEnd: Int): CharSequence {
if (source.isNullOrEmpty()) { //Delete. Do nothing.
return source ?: ""
} else {
val p1 = dest?.toString()?.take(dStart) ?: ""
val p2 = dest?.takeLast(dest.toString().length - dEnd)
val endString = p1 + source + p2
if (endString == "1" || endString == "2" ||
endString.startsWith("19") ||
endString.startsWith("20")) {
return source
} else {
return ""
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment