Format Phone number in Textview - android

Asked

Viewed 1,170 times

2

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

  • Hello Matthew, are you using some framework for development? Phonegap or other? Pure Java? Put a snippet of your code so we know at least what this textView looks like.

  • @Pauloroberto edited the question with more information.

  • 1

    A Jquery or Javascript would help you in this case? Or would it have to be something in java anyway?

  • From what I understand, you have a number with no format and want to somehow display this number on a TextView with the given format, is that it? When you say "mask", I understand it to be in a EditText, so that when the user is typing, the format is applied.

  • Exact @Paulorodrigues, really Mask refers to Edittext, I changed the question.

  • @Pauloroberto has to be java

  • The number is available in what way to you? Like three integers or a long? Or string?

Show 2 more comments

2 answers

1

I used to do this:

I have static methods in my Phonemaskcontroller class, which clean and add 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 edittext-working Fragment and/or Activity, 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) {
            }
        }
    };

1

Whereas the number is in one 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);

The divisions remove the right part of the number while the rest remove the left part of the long. I used long for the int not 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);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.