Skip to content

Instantly share code, notes, and snippets.

@shohiebsense
Last active August 29, 2021 00:20
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 shohiebsense/495e4f7fa6262e9a746fae0d59159653 to your computer and use it in GitHub Desktop.
Save shohiebsense/495e4f7fa6262e9a746fae0d59159653 to your computer and use it in GitHub Desktop.
Prevents Multiple Spaces And Auto Capitalization On TextField
val text = mutableListOf("")
fun onValueChanged(value: String) {
if (!value.isName()) return
val filteredValue = value.replace("\\s+".toRegex(), " ")
if (value.contains("\n")) return
text.value = value
}
fun onFocusChanged(isFocused: Boolean) {
val newValueList= text.value.trim().splitToSequence(' ').filter { it.isNotBlank() }.toMutableList()
newValueList.forEachIndexed { i, word ->
var newWord = ""
word.forEachIndexed { index, c ->
newWord += if(index == 0) c.uppercase()
else c.lowercase()
}
newValueList[i] = newWord
}
text.value = newValueList.joinToString(separator = " ")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment