Thursday, May 1, 2014

Phone number formatting (xxx-xxx-xxxx) in Android

After a long time, I am sharing a small code snippet, by using that we can format the 10 digit phone number in xxx-xxx-xxxx. I found some libraries, which validates the number as well (https://code.google.com/p/libphonenumber/). And other one, Default android util ie.http://developer.android.com/reference/android/telephony/PhoneNumberUtils.html. But this is not a matured API to format every case like the numbers starts with "1" etc. So, thought of sharing the code snippet which uses default Text watcher to do this. Code is as follows


public class PhoneNumberTextWatcher implements TextWatcher {

private static final String TAG = PhoneNumberTextWatcher.class
        .getSimpleName();
private EditText edTxt;
private boolean isDelete;

public PhoneNumberTextWatcher(EditText edTxtPhone) {
    this.edTxt = edTxtPhone;
    edTxt.setOnKeyListener(new OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_DEL) {
                isDelete = true;
            }
            return false;
        }
    });
}

public void onTextChanged(CharSequence s, int start, int before, int count) {
}

public void beforeTextChanged(CharSequence s, int start, int count,
        int after) {
}

public void afterTextChanged(Editable s) {

    if (isDelete) {
        isDelete = false;
        return;
    }
    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 (a.length() == 3) {
                stringBuffer.append("-");
            }
        }
        if (b != null && b.length() > 0) {
            stringBuffer.append(b);
            if (b.length() == 3) {
                stringBuffer.append("-");
            }
        }
        if (c != null && c.length() > 0) {
            stringBuffer.append(c);
        }
        edTxt.removeTextChangedListener(this);
        edTxt.setText(stringBuffer.toString());
        edTxt.setSelection(edTxt.getText().toString().length());
        edTxt.addTextChangedListener(this);
    } else {
        edTxt.removeTextChangedListener(this);
        edTxt.setText("");
        edTxt.addTextChangedListener(this);
    }

 }
}
We can call this Util by using the following line of code,

EditText edTxtPhoneNumber = ((EditText) frLayout.findViewById(R.id.rightHeaderTxt))
edTxtPhoneNumber.addTextChangedListener(new PhoneNumberTextWatcher(edTxtPhoneNumber));

Hope this helps. Thanks in advance.

7 comments:

  1. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. Welcome,
      Please up vote the same answer in the stack overflow thread.

      http://stackoverflow.com/questions/6142813/getting-phone-number-in-xxx-xxx-xxxx-format-from-edittext-box-in-android/23383939#23383939

      Delete
  2. Hi..Any solution for the stuck issue while deleting - character?

    ReplyDelete