Android - I want to get a number input from the user into an EditText - it needs to be separated by spaces - every 4 characters. Example: 123456781234 -> 1234 5678 1234
This is only for visual purpose. However i need the string without spaces for further usage.
What is the easiest way I can do this?
You need to use TextWatcher to achieve visual purpose spaces.
And use any simply split string by space logic to join it back or loop through the entire string per character wise and eliminate
(char) 32
from the stringFormat of text is 000 000 0000
android edittext textwatcher format phone number like xxx-xxx-xx-xx
I have created a class that encapsulates the given behavior.
Here's a snippet on how to use it:
Simple Answer
is this editext for credit card?
first create count variable
then put this in your oncreate(activity) / onviewcreated(fragment)
Assuming that you know the final length of the String, you could implement a
TextWatcher
this way:You just add a space each 4-digits block.
p2 == 0
is to assure the user is not deleting, otherwise he/she would get stock.The code is in Kotlin, You can do it exactly the same way in Java.