ADT
ADT

Reputation: 255

Android Get Country Name with its code

I want to get the country name and country code. For that, I have used the code below:

Country_Language = getApplicationContext().getResources().getConfiguration().locale.getDisplayLanguage();
Country_code= getApplicationContext().getResources().getConfiguration().locale.getCountry(); // get country code

But I always get "US" and "United State" as the country code and country name, while my country is not US.

Any help is appreciated.

Upvotes: 0

Views: 13223

Answers (3)

Subramanian Ramsundaram
Subramanian Ramsundaram

Reputation: 1347

Try this:

TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String countryCode = tm.getSimCountryIso();

Upvotes: 1

Dev Techno
Dev Techno

Reputation: 1

But I always get "US" and "United State" as the country code and country name, while my country is not US.

Any help is appreciated.

It happens because you run this code on without SIM device. To solve this problem, you should run this code on a SIM device, after that this code will show current country name and code of device.

Upvotes: 0

Hareshkumar Chhelana
Hareshkumar Chhelana

Reputation: 24848

//try this
**HardCode Local Configure**
Locale locale = new Locale("en","South Africa");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
                getBaseContext().getResources().getDisplayMetrics());


String country_Language = getApplicationContext().getResources().getConfiguration().locale.getDisplayLanguage();
String Country_code= getApplicationContext().getResources().getConfiguration().locale.getCountry();

System.out.println("County Language :"+country_Language);
System.out.println("County Code :"+Country_code);

Upvotes: 0

Related Questions