Format Cell Phone Number in TextView - android

2

I have TextView that displays the user's phone number, I would like to display in my application a mask that would leave the phone number in (xx) xxxx-xxxxx format. How could I do that?

    
asked by anonymous 07.10.2015 / 20:22

2 answers

1

I consistently do this:

I have the static methods in my PhoneMaskController class, which clean and add the formatting:

public static String clearFormating(String phoneNumber) {
    phoneNumber = phoneNumber.replace("(", "").replace(")", "").replace("-", "")
            .replace(" ", "");
    return phoneNumber;
}

public static String formatPhoneNumber(String phoneNumber) {
    StringBuilder sb = new StringBuilder();
    if (phoneNumber.length() >= 5 && phoneNumber.length() < 9) {
        sb.append(phoneNumber.subSequence(0, 4));
        sb.append('-');
        sb.append(phoneNumber.subSequence(4, phoneNumber.length()));
    } else if (phoneNumber.length() == 9) {

        sb.append(phoneNumber.subSequence(0, 5));
        sb.append('-');
        sb.append(phoneNumber.subSequence(5, phoneNumber.length()));

    } else if (phoneNumber.length() == 10) {

        sb.append("(");
        sb.append(phoneNumber.subSequence(0, 2));
        sb.append(") ");
        sb.append(phoneNumber.subSequence(2, 6));
        sb.append("-");
        sb.append(phoneNumber.subSequence(6, phoneNumber.length()));

    } else if (phoneNumber.length() == 11) {
        if (phoneNumber.startsWith("0")) {
            sb.append("(");
            sb.append(phoneNumber.subSequence(0, 3));
            sb.append(") ");
            sb.append(phoneNumber.subSequence(3, 7));
            sb.append("-");
            sb.append(phoneNumber.subSequence(7, phoneNumber.length()));

        } else {
            sb.append("(");
            sb.append(phoneNumber.subSequence(0, 2));
            sb.append(") ");
            sb.append(phoneNumber.subSequence(2, 7));
            sb.append("-");
            sb.append(phoneNumber.subSequence(7, phoneNumber.length()));
        }

    } else if (phoneNumber.length() == 12) {
        if (phoneNumber.startsWith("0")) {
            sb.append("(");
            sb.append(phoneNumber.subSequence(0, 3));
            sb.append(") ");
            sb.append(phoneNumber.subSequence(3, 8));
            sb.append("-");
            sb.append(phoneNumber.subSequence(8, phoneNumber.length()));

        } else {
            sb.append("(");
            sb.append(phoneNumber.subSequence(0, 2));
            sb.append(" ");
            sb.append(phoneNumber.subSequence(2, 4));
            sb.append(") ");
            sb.append(phoneNumber.subSequence(4, 8));
            sb.append("-");
            sb.append(phoneNumber.subSequence(8, phoneNumber.length()));
        }

    } else if (phoneNumber.length() == 13) {
        if (phoneNumber.startsWith("0")) {
            sb.append("(");
            sb.append(phoneNumber.subSequence(0, 3));
            sb.append(" ");
            sb.append(phoneNumber.subSequence(3, 5));
            sb.append(") ");
            sb.append(phoneNumber.subSequence(5, 9));
            sb.append("-");
            sb.append(phoneNumber.subSequence(9, phoneNumber.length()));
        } else {
            sb.append("(");
            sb.append(phoneNumber.subSequence(0, 2));
            sb.append(" ");
            sb.append(phoneNumber.subSequence(2, 4));
            sb.append(") ");
            sb.append(phoneNumber.subSequence(4, 9));
            sb.append("-");
            sb.append(phoneNumber.subSequence(9, phoneNumber.length()));
        }

    } else if (phoneNumber.length() == 14) {
        sb.append("(");
        sb.append(phoneNumber.subSequence(0, 3));
        sb.append(" ");
        sb.append(phoneNumber.subSequence(3, 5));
        sb.append(") ");
        sb.append(phoneNumber.subSequence(5, 10));
        sb.append("-");
        sb.append(phoneNumber.subSequence(10, phoneNumber.length()));

    } else {
        sb.append(phoneNumber);
    }
    return sb.toString();
}

In my fragment and / or activity that is working with edittext, I add the following parameter in edittext:

yourEdittext.addTextChangedListener(filterTextWatcherPhoneNumber);

And the following method:

 private TextWatcher filterTextWatcherPhoneNumber = new TextWatcher() {
        public void afterTextChanged(Editable s) {

        }

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

        public void onTextChanged(CharSequence s, int start, int before,
                                  int count) {
            try {
                if (isUpdatingNumber) {
                    isUpdatingNumber = false;
                    return;
                }

                String result = PhoneMaskController.clearFormating(s.toString());
                if (result.length() < 15) {
                    result = PhoneMaskController.formatPhoneNumber(result);
                } else {
                    result = result.substring(0, 15);
                    result = PhoneMaskController.formatPhoneNumber(result);
                }
                isUpdatingNumber = true;
                itemUserPhone.setText(result);
                itemUserPhone
                        .setSelection(itemUserPhone.getText().length());
            } catch (Exception e) {
            }
        }
    };
    
08.10.2015 / 21:33
1

Since the number is in long , you can use String.format() :

long telefone = 55123456789L; // (55) 1234-56789
// (xx) xxxx-xxxxx
String s = String.format("(%02d) %04d-%05d",
   (telefone / 1000000000L) % 100, (telefone / 10000) % 10000, telefone % 100000);
textView.setText(s);

Divisions remove the right part of the number while the remainder removes the left part of long . I've used long since int does not have enough bits for 11 digits of the number.

If the number is available in three integers, then you can use:

// (xx) xxxx-xxxxx
// (99) 1234-56789
int codigo = 99;
int esquerda = 1234;
int direita = 56789;

String s = String.format("(%02d) %04d-%05d", codigo, esquerda, direita);
textView.setText(s);
    
05.02.2017 / 03:48