Skip to content

Instantly share code, notes, and snippets.

@VAdaihiep
Created March 22, 2016 03:54
Show Gist options
  • Save VAdaihiep/d7caa2fe8621381886e5 to your computer and use it in GitHub Desktop.
Save VAdaihiep/d7caa2fe8621381886e5 to your computer and use it in GitHub Desktop.
EditText auto add space when input phone number (phone number in Vietnam). Example: 01234567890 -> 0123 456 7890
import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.DigitsKeyListener;
import android.util.AttributeSet;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
/**
* Created by AnhNV@mwork.vn on 22/03/2016.
*/
public class PhoneNumberEditText extends EditText {
TextWatcher textWatcher = 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) {
String phoneNumber = getText().toString();
phoneNumber = phoneNumber.replaceAll(" ", "");
if (!validatePhoneNumber(phoneNumber)) {
setTextColor(getResources().getColor(R.color.red));
} else {
setTextColor(getResources().getColor(R.color.green_darker));
}
if (isFormattedPhone(s.toString())) {
return;
}
String formatted = formatPhoneNumber(s.toString());
setText(formatted);
setSelection(formatted.length());
}
@Override
public void afterTextChanged(Editable s) {
}
};
public PhoneNumberEditText(Context context) {
super(context);
init();
}
public PhoneNumberEditText(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public PhoneNumberEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init(){
addTextChangedListener(textWatcher);
setInputType(EditorInfo.TYPE_CLASS_PHONE);
setKeyListener(DigitsKeyListener.getInstance("0123456789"));
setSingleLine();
}
/**
* @return
*/
private boolean validatePhoneNumber(String phoneNumber) {
boolean result = true;
// Phone
String regexPhone = "(\\+\\d{2,4})?\\s?(\\d{6,15})";
if (TextUtils.isEmpty(phoneNumber) || !phoneNumber.matches(regexPhone)) {
result = false;
}
return result;
}
private boolean isFormattedPhone(String rawPhone) {
String[] separate = rawPhone.split(" ");
if (separate == null || separate.length == 0) {
return true;
}
if (separate.length == 1) {
if (separate[0].length() <= 4) {
return true;
} else {
return false;
}
}
if (separate.length == 2) {
if (separate[0].length() == 4 && separate[1].length() <= 3) {
return true;
} else {
return false;
}
}
if (separate.length >= 3) {
if (separate[0].length() == 4 && separate[1].length() == 3) {
return true;
} else {
return false;
}
}
return true;
}
/**
* 0123456789xxx -> 0123 456 789xxx
*
* @param rawPhone
* @return
*/
private String formatPhoneNumber(String rawPhone) {
rawPhone = rawPhone.replaceAll(" ", "");
String phoneFormat = "";
if (rawPhone.length() > 4) {
phoneFormat += rawPhone.substring(0, 4);
rawPhone = rawPhone.substring(4);
} else {
return rawPhone;
}
phoneFormat += " ";
if (rawPhone.length() > 3) {
phoneFormat += rawPhone.substring(0, 3);
rawPhone = rawPhone.substring(3);
} else {
phoneFormat += rawPhone;
return phoneFormat;
}
phoneFormat += " " + rawPhone;
return phoneFormat;
}
public String getPhoneNumber() {
return getText().toString().trim().replaceAll(" ", "");
}
}
@headhard0024
Copy link

1234567890

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