Skip to content

Instantly share code, notes, and snippets.

@chydee
Created August 26, 2021 14:49
Show Gist options
  • Save chydee/29bffc30a52b8b257fd64c84a0540553 to your computer and use it in GitHub Desktop.
Save chydee/29bffc30a52b8b257fd64c84a0540553 to your computer and use it in GitHub Desktop.
OTP Behaviour
private fun clearInputs() {
val colorStateList = ColorStateList.valueOf(Color.RED)
binding.apply {
otp1.text.clear()
otp2.text.clear()
otp3.text.clear()
otp4.text.clear()
otp5.text.clear()
otp6.text.clear()
otp1.backgroundTintList = colorStateList
otp2.backgroundTintList = colorStateList
otp3.backgroundTintList = colorStateList
otp4.backgroundTintList = colorStateList
otp5.backgroundTintList = colorStateList
otp6.backgroundTintList = colorStateList
}
setDefaultInputFocus()
}
private fun setDefaultInputFocus() {
binding.apply {
otp6.clearFocus()
otp5.clearFocus()
otp4.clearFocus()
otp3.clearFocus()
otp2.clearFocus()
otp1.requestFocus()
}
}
private fun sharedFocus() {
binding.otp1.addTextChangedListener(object : TextWatcher {
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
if (sb.isEmpty() && binding.otp1.length() == 1) {
sb.append(s)
binding.otp1.clearFocus()
binding.otp2.requestFocus()
}
}
override fun beforeTextChanged(
s: CharSequence, start: Int, count: Int,
after: Int
) {
if (sb.length == 1) {
sb.deleteCharAt(0)
}
}
override fun afterTextChanged(s: Editable) {
if (sb.isEmpty()) {
binding.otp1.requestFocus()
}
}
})
binding.otp2.addTextChangedListener(object : TextWatcher {
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
if (sb.isNotEmpty() && binding.otp2.length() == 1) {
sb.append(s)
binding.otp2.clearFocus()
binding.otp3.requestFocus()
}
}
override fun beforeTextChanged(
s: CharSequence, start: Int, count: Int,
after: Int
) {
if (sb.length == 2) {
sb.deleteCharAt(0)
}
}
override fun afterTextChanged(s: Editable) {
if (sb.isEmpty()) {
binding.otp1.requestFocus()
}
}
})
binding.otp3.addTextChangedListener(object : TextWatcher {
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
if (sb.isNotEmpty() && binding.otp3.length() == 1) {
sb.append(s)
binding.otp3.clearFocus()
binding.otp4.requestFocus()
}
}
override fun beforeTextChanged(
s: CharSequence, start: Int, count: Int,
after: Int
) {
if (sb.length == 3) {
sb.deleteCharAt(0)
}
}
override fun afterTextChanged(s: Editable) {
if (sb.isEmpty()) {
binding.otp1.requestFocus()
}
}
})
binding.otp4.addTextChangedListener(object : TextWatcher {
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
if (sb.isNotEmpty() && binding.otp4.length() == 1) {
sb.append(s)
binding.otp4.clearFocus()
binding.otp5.requestFocus()
}
}
override fun beforeTextChanged(
s: CharSequence, start: Int, count: Int,
after: Int
) {
if (sb.length == 4) {
sb.deleteCharAt(0)
}
}
override fun afterTextChanged(s: Editable) {
if (sb.isEmpty()) {
binding.otp1.requestFocus()
}
}
})
binding.otp5.addTextChangedListener(object : TextWatcher {
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
if (sb.isNotEmpty() && binding.otp5.length() == 1) {
sb.append(s)
binding.otp5.clearFocus()
binding.otp6.requestFocus()
}
}
override fun beforeTextChanged(
s: CharSequence, start: Int, count: Int,
after: Int
) {
if (sb.length == 5) {
sb.deleteCharAt(0)
}
}
override fun afterTextChanged(s: Editable) {
if (sb.isEmpty()) {
binding.otp1.requestFocus()
}
}
})
binding.otp6.addTextChangedListener(object : TextWatcher {
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
if (sb.isNotEmpty() && binding.otp6.length() == 1) {
sb.append(s)
binding.otp6.clearFocus()
}
}
override fun beforeTextChanged(
s: CharSequence, start: Int, count: Int,
after: Int
) {
if (sb.length == 6) {
sb.deleteCharAt(0)
}
}
override fun afterTextChanged(s: Editable) {
if (sb.isEmpty()) {
binding.otp1.requestFocus()
}
if (sb.length == 6) {
binding.otp6.clearFocus()
hideSoftKeyboard()
binding.submitButton.requestFocus()
}
}
})
}
/**
* A TextWatcher object
*/
private val mTextWatcher: TextWatcher = object : TextWatcher {
override fun beforeTextChanged(charSequence: CharSequence, i: Int, i2: Int, i3: Int) {}
override fun onTextChanged(charSequence: CharSequence, i: Int, i2: Int, i3: Int) {}
override fun afterTextChanged(editable: Editable) {
// check Fields For Empty Values
checkFieldsForEmptyValues()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment