Last active
November 30, 2021 08:50
-
-
Save rajohns08/f28ee3b97a545249e5f7 to your computer and use it in GitHub Desktop.
Android - Add hyphens to an SSN EditText field while the user is typing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ssnEditText.addTextChangedListener(new TextWatcher() { | |
@Override | |
public void beforeTextChanged(CharSequence s, int start, int count, int after) { | |
} | |
@Override | |
public void onTextChanged(CharSequence s, int start, int before, int count) { | |
// if user is typing string one character at a time | |
if (count == 1) { | |
// auto insert dashes while user typing their ssn | |
if (start == 2 || start == 5) { | |
ssnEditText.setText(ssnEditText.getText() + "-"); | |
ssnEditText.setSelection(ssnEditText.getText().length()); | |
} | |
} | |
} | |
@Override | |
public void afterTextChanged(Editable s) { | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I userf it, but can't remove added hyphens when key down Backspace.
How to remove added hyphens?