1

I want to type phone number in EditText in format (XXX)-XXX-XXXX. But I am not getting any solution for this. I succeeded to type but when I use backspace and again type number then this format not comes.Please give me solution. My code is

phoneEditText.addTextChangedListener(new TextWatcher() {
            private int keyDel;

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

                phoneEditText.setOnKeyListener(new OnKeyListener() {

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

                if (keyDel == 0) {
                    int len = phoneEditText.getText().length();
                    if (len == 3
                            && !(phoneEditText.getText().toString()
                                    .contains("("))) {
                        phoneEditText.setText("("
                                + phoneEditText.getText().toString().trim()
                                + ")-");
                        phoneEditText.setSelection(phoneEditText.getText()
                                .length());
                    } else if ((len == 1 || len == 2 || len == 4 || len == 3)
                            && (phoneEditText.getText().toString()
                                    .contains("("))) {
                        if (len == 4)
                            phoneEditText.setText(phoneEditText.getText()
                                    .toString().trim()
                                    + ")-");
                        phoneEditText.setSelection(phoneEditText.getText()
                                .length());

                    } else if (len > 3 && len == 9) {
                        phoneEditText.setText(phoneEditText.getText()
                                .toString().trim()
                                + "-");
                        phoneEditText.setSelection(phoneEditText.getText()
                                .length());
                    } else if (len > 9 && len == 14) {
                    }

                } else {
                    keyDel = 0;
                }
            }

            @Override
            public void afterTextChanged(Editable arg0) {
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1,
                    int arg2, int arg3) {
            }
        });
Kara
  • 6,115
  • 16
  • 50
  • 57
user3273769
  • 33
  • 1
  • 1
  • 7

2 Answers2

1

Take a look at PhoneNumberUtils for more options.

String formattedNumber = PhoneNumberUtils.formatNumber(unformattedNumber);

This will automatically format the number according to the rules for the country the number is from.

You can also format Editable text in-place using:

PhoneNumberUtils.formatNumber(Editable text, int defaultFormattingType);

OR Use this refer to https://code.google.com/p/libphonenumber/:

PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
try {
  PhoneNumber swissNumberProto = phoneUtil.parse(swissNumberStr, "US");
  String Phone=phoneUtil.format(edt.getText().toString(), PhoneNumberFormat.NATIONAL)
} catch (NumberParseException e) {
  System.err.println("NumberParseException was thrown: " + e.toString());
}
Mohammad Rababah
  • 1,730
  • 4
  • 17
  • 32
  • I have tried each option no one working..Phone number utils not change phone number on runtime. I want If user type number then on runtime format implemented on edittext. – user3273769 Feb 05 '14 at 08:42
  • Thanks. But I think this code is for fixed number its not my format which I want. – user3273769 Feb 05 '14 at 08:58
  • I have posted my answer in another thread, http://stackoverflow.com/questions/6142813/getting-phone-number-in-xxx-xxx-xxxx-format-from-edittext-box-in-android/23383939#23383939 – jrh May 23 '14 at 05:44
0

Have you tried PhoneNumberFormattingTextWatcher from android.telephony package?

You can easily add it to an EditText and it formats the text using current locale or a country code.

From android docs:

Watches a TextView and if a phone number is entered will format it.

Stop formatting when the user

  • Inputs non-dialable characters

  • Removes the separator in the middle of string.

The formatting will be restarted once the text is cleared.

Here is a sample code:

editText.addTextChangedListener(new PhoneNumberFormattingTextWatcher());

Also don't forget to set your edittext's input type to phone.

Hope that it helps.

SafaOrhan
  • 690
  • 9
  • 19