I am developing one application in android for California. now i want to enter Social Security Number in Edit Text in xxx-xx-xxxx format. i have tried following code for achieve the functionality but it is not working fine for clearing the text.. following is my code. any help would be appreciated. i want auto format functionality.
etSocialSecurityNumber.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { String text = etSocialSecurityNumber.getText().toString(); textlength = etSocialSecurityNumber.getText().length(); if(text.endsWith(" ")) return; if(textlength == 4 || textlength == 7 ) { etSocialSecurityNumber.setText(new StringBuilder(text).insert(text.length()-1, "-").toString()); etSocialSecurityNumber.setSelection(etSocialSecurityNumber.getText().length()); } } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void afterTextChanged(Editable s) { } });This will add a - after every 4 characters and it handels all kind of editing of user, just put your condition for 3 and 2 chars:
edit_text.addTextChangedListener(new TextWatcher() { private boolean spaceDeleted; public void onTextChanged(CharSequence s, int start, int before, int count) { } public void beforeTextChanged(CharSequence s, int start, int count, int after) { CharSequence charDeleted = s.subSequence(start, start + count); spaceDeleted = " ".equals(charDeleted.toString()); } public void afterTextChanged(Editable editable) { edit_text.removeTextChangedListener(this); int cursorPosition = edit_text.getSelectionStart(); String withSpaces = formatText(editable); edit_text.setText(withSpaces); edit_text.setSelection(cursorPosition + (withSpaces.length() - editable.length())); if (spaceDeleted) { edit_text.setSelection(edit_text.getSelectionStart() - 1); spaceDeleted = false; } edit_text.addTextChangedListener(this); } private String formatText(CharSequence text) { StringBuilder formatted = new StringBuilder(); int count = 0; for (int i = 0; i < text.length(); ++i) { if (Character.isDigit(text.charAt(i))) { if (count % 4 == 0 && count > 0) formatted.append("-"); formatted.append(text.charAt(i)); ++count; } } return formatted.toString(); } });Edited:
userNameET.addTextChangedListener(new TextWatcher() { private boolean spaceDeleted; public void onTextChanged(CharSequence s, int start, int before, int count) { } public void beforeTextChanged(CharSequence s, int start, int count, int after) { CharSequence charDeleted = s.subSequence(start, start + count); spaceDeleted = "-".equals(charDeleted.toString()); } public void afterTextChanged(Editable editable) { userNameET.removeTextChangedListener(this); int cursorPosition = userNameET.getSelectionStart(); String withSpaces = formatText(editable); userNameET.setText(withSpaces); userNameET.setSelection(cursorPosition + (withSpaces.length() - editable.length())); if (spaceDeleted) { // userNameET.setSelection(userNameET.getSelectionStart() - 1); spaceDeleted = false; } userNameET.addTextChangedListener(this); } private String formatText(CharSequence text) { StringBuilder formatted = new StringBuilder(); int count = 0; if(text.length()==3||text.length()==6) { if (!spaceDeleted) formatted.append(text+"-"); else formatted.append(text); } else formatted.append(text); return formatted.toString(); } });