I would like to know how can i get the device/user area code from within my android app I know there is a lib named TelephonyManager but i couldn't find the country code feature
To make myself clear: I have an activity which asks the user to enter his phone number and i want the area code to be taken from the device (from his carrier?) If i would open my app in new york i would get +212 as the code and a text box to enter my own number
Use
compile 'com.googlecode.libphonenumber:libphonenumber:8.0.1'
dependency. This makes the task easy and provides accurate result because it's by google.Here you can know more about it.
The output I m getting is something like this:
Here I m using spinner.
1) I have created a custom Adapter CountryCodesAdapter:
2) Now the layout of my Activity where I am displaying the spinner of country code is:
3) java Activity code is below:
Few extra features:
a) Your country will get automatically selected in the spinner.
To achieve I have used this:
I think this method is little better than other ways listed here. So I posted this. Hope someone may find it helpful :-)
This project on GitHub is here
In class TelephonyManager, I find a method
public String getNetworkCountryIso ()
, this may be what you want. Also, there are some other method which can return more info about the phone. You may read http://en.wikipedia.org/wiki/List_of_mobile_country_codes to see which country is represented by a MCC.This question helped me so much, I want to give you all a class that provide country code from phone code and viceversa.
Usage:
CountryCodes.getCode(String country)
returns international code from country signCountryCodes.getCountry(String code)
returns country signs from international codeAn instance of this class can be provided as adapter to Views, for example in Activity onCreate:
What follows is the class code
I'm using it and seems to work but, you know, just death and taxes are certain. Feel free to customize it and suggest fixes.
HTH
If you happen to use Google's libphonenumber anyway, and if you work with phone numbers, it's rather likely you do, you can use its internal data that describes the same. It's not public, so you have to use reflection to reach it:
Then you can search or process that map to get the data you want.
Sorry to revive a dead thread, but I spent a while trying to solve this issue so hopefully my response will help someone else.
The example in the original question is confusing, you say you want to append the country code, but give New York area code 212 as an example; That is not a country code it is a local area code. The country code for USA is 1. So you would want the final number to look like "1-212-xxx-xxxx". My answer below explains how to determine the country code, not the local area code.
To get the country calling code for your own number, use getSimCountryIso(). Do not use getNetworkCountryIso(); getNetworkCountryIso will return the country that you are currently located in, but if you are travelling this will return the wrong country. For example if you are from Albania but are travelling in New York getSimCountryIso will return "al", but getNetworkCountryIso will return "us". Obviously if you use the country code for the USA, you have the wrong number.
Now you have a two character country code; for example Canada will be "ca". You can then use this as a lookup key for the table below (obtained from http://www.sikosoft.com/item/international_telephone_country_codes_in_xml_format_using_iso_639-1).
Since country codes don't ever change, it is acceptable to hard code a lookup table in your application. Unfortunately there doesn't seem to be a direct way to get the user's current country code.
add these to the string.xml Enjoy:)