I have a number that I need to format as a telephone number. If I do
PhoneNumberUtils.formatNumber(numStr);
Then I get
888-555-1234
But what I need to get is
(888) 555-1234
How do I get the second one? Is there a standard android way?
If you know the country for which you want to do it, you can use Google's open source library libphonenumber . Here is how you can format it:
String numberStr = "8885551234"
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
try {
PhoneNumber numberProto = phoneUtil.parse(numberStr, "US");
//Since you know the country you can format it as follows:
System.out.println(phoneUtil.format(numberProto, PhoneNumberFormat.NATIONAL));
} catch (NumberParseException e) {
System.err.println("NumberParseException was thrown: " + e.toString());
}
If you don't know the country then for numberStr
use E.164 format phone number and in place of country code use null.