Reputation: 27
I need to access the ISO3 Country code based on the country entered by the user for my android application. Is there any possible way to access it directly rather than storing it in an array.
Upvotes: 1
Views: 3245
Reputation: 2938
You can get the current ISO2 country code using telephony manager or by using IP address. Then fetch the ISO3 country code using Locale.
Here is the sample:
TelephonyManager tm = (TelephonyManager)getSystemService(getApplicationContext().TELEPHONY_SERVICE);
String countryCode = tm.getNetworkCountryIso();
String currentISO3CountryCode = new Locale("", countryCode).getISO3Country();
Also here is the sample to get all country's ISO3 code:
for (String country : Locale.getISOCountries()) {
Locale locale = new Locale("", country);
Log.v(TAG, "ISO3 country code: " + locale.getISO3Country().toUpperCase());
}
Upvotes: 3