Skip to content

Instantly share code, notes, and snippets.

@rajohns08
Last active November 30, 2021 08:50
Show Gist options
  • Save rajohns08/f28ee3b97a545249e5f7 to your computer and use it in GitHub Desktop.
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
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) {
}
});
@ruNNiNg-V0V
Copy link

I userf it, but can't remove added hyphens when key down Backspace.
How to remove added hyphens?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment