user3273769
user3273769

Reputation: 33

Android Phone number format (XXX)-XXX-XXXX

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) {
            }
        });

Upvotes: 1

Views: 20998

Answers (2)

SafaOrhan
SafaOrhan

Reputation: 720

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.

Upvotes: 0

Mohammad Rababah
Mohammad Rababah

Reputation: 1726

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());
}

Upvotes: 1

Related Questions