Last active
December 13, 2019 21:57
-
-
Save sgallego/1e63d1356f2815d1a12fd2bfa095d171 to your computer and use it in GitHub Desktop.
InputFilter to enter years from 1900 onwards
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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