user1746619 user1746619 - 1 month ago 14
Android Question

Phone number format in android

In my application, I have a editText which will accept phone number from the user, My target is, as soon as user enters the phone number it should be formatted (Like by applying on text changed listener) , format is like

XXX-XXX-XXXX
.

I wrote the code as

ePhone.addTextChangedListener(new TextWatcher() {

private Pattern pattern;
private Matcher matcher;
String a;


@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub

boolean flag = true;
if (flag) {

if (((ePhone.getText().length() + 1) % 4) == 0)
{
if (ePhone.getText().toString().split("-").length <= 2)
{
ePhone.setText(ePhone.getText() + "-");
ePhone.setSelection(ePhone.getText().length());
}
}
a = ePhone.getText().toString();
} else {
ePhone.setText(a);
}
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub

}

@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stus

}
});


But when the user wants to delete a number from this
editText
, its not working properly.
Thanks in advance.

Answer

Supposing that you wanna format the phone number as per US fomat... The following will serve your purpose... .............. formats supported +1 (###) ###-####,1 (###) ###-####,###-####,###-###-####,011 $ ...........................

 phoneEditText.addTextChangedListener(new TextWatcher() {
                private boolean mFormatting; // this is a flag which prevents the  stack overflow.
                private int mAfter;

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    // nothing to do here.. 
                }

                //called before the text is changed...
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                   //nothing to do here...
                    mAfter  =   after; // flag to detect backspace..

                }

                @Override
                public void afterTextChanged(Editable s) {
                    // Make sure to ignore calls to afterTextChanged caused by the work done below
                    if (!mFormatting) {
                        mFormatting = true;
                       // using US formatting...
                        if(mAfter!=0) // in case back space ain't clicked...
                PhoneNumberUtils.formatNumber(s,PhoneNumberUtils.getFormatTypeForLocale(Locale.US));                             
                         mFormatting = false;
                    }
                }
            });

If you need location specific services,ie for each location you need specific format of that place... Refer to http://developer.android.com/reference/android/telephony/PhoneNumberFormattingTextWatcher.html . If you need only the format you needed,then write a custom function in the place of line in the above code snippet...

            PhoneNumberUtils.formatNumber(s,PhoneNumberUtils.getFormatTypeForLocale(Locale.US));     
Comments