Question

I am allowing the user to choose a phone number from their address book. I need the number to always be in the international format, however sometimes people store a local number without the country code in their contacts (ex. 555-555-5555 instead of +1-555-555-5555).

Is there an easy way to find out what country code the local number implies so I can add it manually?

Was it helpful?

Solution

This is what you need https://code.google.com/p/libphonenumber/

String numberString = "044 668 18 00"

PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
try {
  PhoneNumber numberProto = phoneUtil.parse(numberString, "BH"); // Where BH is the user's iso country code
  String finalNumber = phoneUtil.format(numberProto, PhoneNumberFormat.E164);
} catch (NumberParseException e) {
  System.err.println("NumberParseException was thrown: " + e.toString());
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top