In an Android application I have a field where the user should type some 14-digits number like 12345678901234. I want this number to look like 12 3456 7890 1234. I've tried to do this by code:
if((s.length() == 2 || s.length() == 7 || s.length() == 12)){
s.insert(s.length(), " ");
}
But when the user starts to type in the middle of the text, my code works wrong.
I've tried to use the DecimalFormat class:
DecimalFormat decimalFormat = new DecimalFormat("##,####.#### ####");
String formattedText = decimalFormat.format(Double.parseDouble(etContent.getText().toString()));
but I receive an IllegalArgumentException.
Any ideas how to do that?
P.S The main problem is I should format text "right now" like:
1
12
12 3
12 34
12 345
12 3456
12 3456 7
12 3456 78
12 3456 789
12 3456 7890
12 3456 7890 1
12 3456 7890 12
12 3456 7890 123
12 3456 7890 1234