1

I want to fetch phone number X(XXX)XXX-XXX in this format and when I Google I found PhoneNumberUtils.formatNumber() but it gives the number in this format X-XXX-XXX-XXX so how can I get the number in X(XXX)XXX-XXX fromat.

Code

holder.tv_vehicle.setText("Phone #: " + PhoneNumberUtils.formatNumber(data.getUserPhone()));

2 Answers2

0

PhoneNumberUtils.formatNumber will return (XXX)XXX-XXX if you pass a local number and X-XXX-XXX-XXX for international number , in your case you have 2 solutions :

  1. Remove the country code then parse the phone number

         String phoneNumber = "+1-202-000-0000"; 
         String code  = result.substring(0,3);
         String localNumber = result.replace(code,""); //202-555-0196
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
             Log.i("result",code.replace("-","")  + PhoneNumberUtils.formatNumber(localNumber , "US"));
         } else {
             Log.i("result",code.replace("-","") + PhoneNumberUtils.formatNumber(localNumber));
         }
    

Result : +1(202) 000-0000

  1. Use this library google/libphonenumber for formatting international phone numbers.
private String getFormattedNumber(String phoneNumber) {
    
    PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
    
    Phonemetadata.NumberFormat numberFormat = new Phonemetadata.NumberFormat();
    
    numberFormat.pattern = "(\\d{3})(\\d{3})(\\d{4})";
    
    numberFormat.format = "($1) $2-$3";
    
    List<Phonemetadata.NumberFormat> newNumberFormats = new ArrayList<>();
    
    newNumberFormats.add(numberFormat);
    
    Phonenumber.PhoneNumber phoneNumberPN = null;
    
    try {
        phoneNumberPN = phoneNumberUtil.parse(phoneNumber, Locale.US.getCountry());
        phoneNumber = phoneNumberUtil.formatByPattern(phoneNumberPN, PhoneNumberUtil.PhoneNumberFormat.INTERNATIONAL, newNumberFormats);
        
    } catch (NumberParseException e) {
        e.printStackTrace();
    }
    
    return phoneNumber;
}

getFormattedNumber("+12020000000");

Result : +1(202) 000-0000

anehme
  • 536
  • 1
  • 6
  • 18
-1
holder.tv_vehicle.setText("Phone #: " + PhoneNumberUtils.formatNumber(data.getUserPhone()).replaceFirst("-", "(").replaceFirst("-", ")"));

You can use replaceFirst() to insert ( then ) and convert it. I wrote a little bit of a bad code here that should work. If I was doing it in my own program, I would make it into a function that did this and returned the updated string.

Drivers Sea
  • 446
  • 2
  • 10
  • this is kinda scuffed, why even use PhoneNumberUtils.formatNumber in the first place if ur gonna replace this much – DennisVA Sep 18 '21 at 22:07
  • This code run good but @DennisVA is right, then what is the use of `PhoneNumberUtils.formatNumber`? –  Sep 18 '21 at 22:12
  • PhoneNumberUtils can format for a dialer, it can also be used to take a random phone number input and convert it to a base input so you can edit it as you like. If you are getting all the number input in a standard way and you are not putting them into a dialer, then there is likely no reason to use it. – Drivers Sea Sep 18 '21 at 22:44
  • @DriversSea is there any clean way of doing this instead of using `replaceFirst("-", "(").replaceFirst("-", ")")` –  Sep 18 '21 at 22:57