Skip to content

Instantly share code, notes, and snippets.

@jrgavilanes
Created December 12, 2021 20:57
Show Gist options
  • Select an option

  • Save jrgavilanes/a548fea328a92e64543dda5a711feba8 to your computer and use it in GitHub Desktop.

Select an option

Save jrgavilanes/a548fea328a92e64543dda5a711feba8 to your computer and use it in GitHub Desktop.
IBAN Code kotlin simplified version
package com.example.test
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import androidx.core.text.set
import com.example.test.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
setUI()
}
private fun setUI() {
binding.editTextTest.addTextChangedListener(object : TextWatcher {
var _ignore = false
var _deletion = false
override fun afterTextChanged(s: Editable) {
if (_ignore)
return
_ignore = true
val space = ' '
if (!_deletion) {
while (true) {
s.forEachIndexed { index, c ->
if (c == space) {
s.replace(index, index + 1, "")
}
}
s.forEachIndexed { index, c ->
if (index >= 3 && s[index] != ' ' && s[index - 1] != ' ' && s[index - 2] != ' ' && s[index - 3] != ' ') {
s.insert(index + 1, " ")
}
}
break
}
}
_ignore = false
}
override fun beforeTextChanged(
s: CharSequence, start: Int,
count: Int, after: Int
) {
_deletion = after < count
}
override fun onTextChanged(
s: CharSequence, start: Int,
before: Int, count: Int
) {
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment