Last active
December 1, 2022 09:03
-
-
Save pranaypatel512/66b4a6718c07704c1f63 to your computer and use it in GitHub Desktop.
Custom TextWatcher for Accepting PhoneNumber in this (XXX-XXX-XXXX) format.
This file contains 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
import android.text.Editable; | |
import android.text.TextWatcher; | |
import android.widget.EditText; | |
public class PhoneNumberTextWatcher implements TextWatcher { | |
private static final String TAG = "PhoneNumberTextWatcher"; | |
private EditText editText; | |
public PhoneNumberTextWatcher(EditText edTxtPhone) { | |
this.editText = edTxtPhone; | |
} | |
public void onTextChanged(CharSequence s, int cursorPosition, int before, | |
int count) { | |
if (before == 0 && count == 1) { //Entering values | |
String val = s.toString(); | |
String a = ""; | |
String b = ""; | |
String c = ""; | |
if (val != null && val.length() > 0) { | |
val = val.replace("-", ""); | |
if (val.length() >= 3) { | |
a = val.substring(0, 3); | |
} else if (val.length() < 3) { | |
a = val.substring(0, val.length()); | |
} | |
if (val.length() >= 6) { | |
b = val.substring(3, 6); | |
c = val.substring(6, val.length()); | |
} else if (val.length() > 3 && val.length() < 6) { | |
b = val.substring(3, val.length()); | |
} | |
StringBuffer stringBuffer = new StringBuffer(); | |
if (a != null && a.length() > 0) { | |
stringBuffer.append(a); | |
} | |
if (b != null && b.length() > 0) { | |
stringBuffer.append("-"); | |
stringBuffer.append(b); | |
} | |
if (c != null && c.length() > 0) { | |
stringBuffer.append("-"); | |
stringBuffer.append(c); | |
} | |
editText.removeTextChangedListener(this); | |
editText.setText(stringBuffer.toString()); | |
if (cursorPosition == 3 || cursorPosition == 7) { | |
cursorPosition = cursorPosition + 2; | |
} else { | |
cursorPosition = cursorPosition + 1; | |
} | |
if (cursorPosition <= editText.getText().toString().length()) { | |
editText.setSelection(cursorPosition); | |
} else { | |
editText.setSelection(editText.getText().toString().length()); | |
} | |
editText.addTextChangedListener(this); | |
} else { | |
editText.removeTextChangedListener(this); | |
editText.setText(""); | |
editText.addTextChangedListener(this); | |
} | |
} | |
if (before == 1 && count == 0) { //Deleting values | |
String val = s.toString(); | |
String a = ""; | |
String b = ""; | |
String c = ""; | |
if (val != null && val.length() > 0) { | |
val = val.replace("-", ""); | |
if (cursorPosition == 3) { | |
val = removeCharAt(val, cursorPosition - 1, s.toString().length() - 1); | |
} else if (cursorPosition == 7) { | |
val = removeCharAt(val, cursorPosition - 2, s.toString().length() - 2); | |
} | |
if (val.length() >= 3) { | |
a = val.substring(0, 3); | |
} else if (val.length() < 3) { | |
a = val.substring(0, val.length()); | |
} | |
if (val.length() >= 6) { | |
b = val.substring(3, 6); | |
c = val.substring(6, val.length()); | |
} else if (val.length() > 3 && val.length() < 6) { | |
b = val.substring(3, val.length()); | |
} | |
StringBuffer stringBuffer = new StringBuffer(); | |
if (a != null && a.length() > 0) { | |
stringBuffer.append(a); | |
} | |
if (b != null && b.length() > 0) { | |
stringBuffer.append("-"); | |
stringBuffer.append(b); | |
} | |
if (c != null && c.length() > 0) { | |
stringBuffer.append("-"); | |
stringBuffer.append(c); | |
} | |
editText.removeTextChangedListener(this); | |
editText.setText(stringBuffer.toString()); | |
if (cursorPosition == 3 || cursorPosition == 7) { | |
cursorPosition = cursorPosition - 1; | |
} | |
if (cursorPosition <= editText.getText().toString().length()) { | |
editText.setSelection(cursorPosition); | |
} else { | |
editText.setSelection(editText.getText().toString().length()); | |
} | |
editText.addTextChangedListener(this); | |
} else { | |
editText.removeTextChangedListener(this); | |
editText.setText(""); | |
editText.addTextChangedListener(this); | |
} | |
} | |
} | |
public void beforeTextChanged(CharSequence s, int start, int count, | |
int after) { | |
} | |
public void afterTextChanged(Editable s) { | |
} | |
public static String removeCharAt(String s, int pos, int length) { | |
String value = ""; | |
if (length > pos) { | |
value = s.substring(pos + 1); | |
} | |
return s.substring(0, pos) + value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment