Search code examples
androidlocalecountry-codes

Get country name String from country code int


I am new in Java Android.

I would like to get the name of country (String) using country code in Locale (Ref. ISO 3166-1 numeric)

Tried to do something like that (where [...].getCountry() return int 826):

Locale countryName = new Locale("", String.valueOf(profile.getPersonnalInformation().getAddress().getCountry())); 

and get the name of country using this: countryName.getDisplayCountry()

It should normally return me "United Kingdom" but unfortunately I have 826.

How can I do that ?

In advance thank you for your help,


Solution

  • Now an implementation of country code (ISO 3166-1 alpha-2/alpha-3/numeric) list as Java enum is available at GitHub under Apache License version 2.0.

    Example:

    int c_code=profile.getPersonnalInformation().getAddress().getCountry());// 392
    
    CountryCode cc = CountryCode.getByCode(c_code);
    
    String country_name=cc.getName();              // "Japan"
    

    Gradle

    dependencies {
    
      compile 'com.neovisionaries:nv-i18n:1.20'
    
    }
    

    Github

    https://github.com/TakahikoKawasaki/nv-i18n