This question already has an answer here:
I am using libphonenumber api https://code.google.com/p/libphonenumber/ in android project, I am getting country code using following code
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
try {
PhoneNumber numberProto = phoneUtil.parse(number, "IN");
int countryCode = numberProto.getCountryCode();
} catch (NumberParseException e) {
System.err.println("NumberParseException was thrown: " + e.toString());
}
My question is:- -- I have number like +447870000000 but don't know about country code but want to get country code from this number like +44 how can I get it through this api ?
please help me Thanks in advance
If you have a number in a E164 format (in your case +447870000000) and you specify country code ISO in the parser, it will actually ignore the given iso code and do the formatting based on the number.
Country code ISO is needed when the number you give to the method is in national format.
As far as your code snippet goes, it should work.
A nice Library has been provided
https://github.com/hbb20/CountryCodePickerProject
In dependency add
then use this xml code
It will automatically populate the list of country code and also u can search in it.