0

I'm try to make EditText for bank cards. I need to add space after every 4 number. I already tried another answers at stackoverflow but non of them working for me. I try to make it from count (I use textwatcher) but I can't do it. Other answers use insert method for add space but insert method isn't available. When I write insert it become red so I want to ask for learn. How can I make it?

I tried something but I really not know what I'm doing. I really need advices.

Here my editText textwatcher code:

private val textWatcher = object : TextWatcher {

    override fun afterTextChanged(s: Editable?) {

    }
    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
    }
    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {


        var txt = s.toString()

        println(txt)
        println(s!!.length)
        if (s!!.length%4 == 0){
            txt = txt + " "
            println(txt)
        }



    }
}
mehmett
  • 111
  • 1
  • 12
  • helpful link https://stackoverflow.com/questions/10252757/android-format-edittext-to-display-spaces-after-every-4-characters – Wini Dec 03 '20 at 12:50
  • @Wini I saw it but I didn't understand. Can you add Pseudocode for better understanding? – mehmett Dec 03 '20 at 12:53

2 Answers2

1

You are creating a text variable not seting the text to the EditText view

txt = txt + " "

You could think that is modifying the text, but primitives and String are immutable so that is another reference in memory, so even if you are trying to us mutability to achieve it won't work.

if (s!!.length%4 == 0){
    txt = txt + " "
    yourEditText.setText(txt)
}

With EditText you have to use the setter because the field assigned to the text is an Editable so the setText method wraps the conversion from String to Editable

There is a common warning regarding this, if I don't miss remember is also on the docs, modifying during a TextWatcher callback can trigger an infinite loop, in this case, it won't because the change to the text will be "filtered" by the condition.

cutiko
  • 9,887
  • 3
  • 45
  • 59
  • I just try setText but when I added fourth character, going back to the beginning. – mehmett Dec 03 '20 at 12:51
  • If the problem is with the cursor then that is another question, SO is one question at a time. For the cursor, the solution is setting at the end below the `setText` https://stackoverflow.com/a/6624186/4017501 – cutiko Dec 03 '20 at 13:01
  • Problem isn't cursor. When this if loop trigger, text set to edit text but user need countine writing so this isn't work. – mehmett Dec 03 '20 at 13:26
  • `but user need countine writing so this isn't work` why user can't continue writing? What is stop the user from writing after the text is modified? – cutiko Dec 03 '20 at 13:29
  • Because it set text when fourth character wrote so, it isn't relevant to question. In question ask for adding spaces but in that answer not making spaces. This answer only set text when user wrote fourth character on EditText. Thank you for your response. – mehmett Dec 03 '20 at 13:51
  • How is this not space? `txt = txt + " "` there is a space character right there. What do you mean by space? – cutiko Dec 03 '20 at 14:02
  • When we write setText and code executed space become meaningless, let say more clear. When I type 1234, if block triggered which we wrote, and EditText text become 1234 which is great but it was already set. I need to keep writing because space is last character of 1234. So setText method isn't working for me. Not making spaces between numbers, it just add space after fourth character and set text. I hope I can explain myself correctly. I think in here problem is setText because we use onChangeText method so text have to be change. When it is happen I need to add spaces. – mehmett Dec 03 '20 at 14:13
  • Are you using input type number or something like that? – cutiko Dec 03 '20 at 14:44
  • Yes I'm using number for input type – mehmett Dec 03 '20 at 14:46
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/225474/discussion-between-cutiko-and-mehmett). – cutiko Dec 03 '20 at 14:50
0

Just use myedittext.append("") like this

if (s!!.length%4 == 0){ yourEditText.append(" ") }

Irfan Ullah
  • 69
  • 2
  • 9
  • You should read the comments on my answer, I think OP is having problems explaining the actual problem. – cutiko Dec 03 '20 at 13:29