Android library for country flag, currency, and other country information

Overview

World Country Data, flags, currency and more - an open source android library for getting country flags and other country attributes

CodeFactor Android CI with Gradle


An Android library that contains 'all' the flags of the countries of the world This is to be used for android projects where the developer is interested in getting the flag of a particular country for any reason.

  • A flag is obtained as a drawable resource (int).
  • A flag can be set to an ImageView using XML
  • There is possibility to get all the countries and their flags by invoking just two methods.

System requirement

  • Android minSDKversion = 15
  • Android targetSDKversion = 30

Usage

  1. Add JitPack in your repository build file build.gradle (Project appname)
allprojects {
    repositories {
        //...
        maven { url 'https://jitpack.io' }
    }
}

2.1 Add the dependency in your build.gradle (Module: app)

dependencies {
    //...
    implementation 'com.github.blongho:worldCountryData:$version'
}

Replace $version with vXXX for the most stable version you want to use see releases

Proguard rules

2.2 Add this in your proguard-rules.pro

-keep class com.blongho.** {*;}
-keep interface com.blongho.**
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
-keeppackagenames com.blongho.country_data
-keepclassmembers class com.blongho.country_data.* {
   public *;
}
-keep class com.blongho.country_data.R$*{
    *;
 }

2.3 In your build.gradle (Module: app)

android {
    ...

    buildTypes {
        release {
            minifyEnabled true
            //shrinkResources false // if you set this to true, the application will crash
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
  
    }

    defaultConfig {
        vectorDrawables.useSupportLibrary = true 
    }
 ...
}

  1. Build your project (and make sure gradle has successfully synced) Buid >> Clean Project, Build >> Rebuild Project

  2. Load all the flags of the world by calling. Do this once in the application context.

World.init(getApplicationContext()); // Initializes the libray and loads all data

This inititializes the data. All countries are read, and their flags loaded

  1. Get the flag of a country(dynamically)
  • You can get the flag of a country by using the two iso alpha2 or alpha3 or the country name or the numeric code.
// Demonstrating with Sweden
//The attribute is case-insensitive "se == SE == sE == Se"

// use alpha2
final int flag = World.getFlagOf("se"); // use "se" or "sE" or "SE" or "Se"

// use alpha3
final int flag = World.getFlagOf("swe");

// Use country name
final int flag = World.getFlagOf("sweden");

// use country name
final int flag = World.getFlagOf(752);

// Set the image of an imageView
final ImageView swedishFlag= (ImageView) findViewById(R.id.flagImageView);
swedishFlag.setImageResource(flag);

/*
The value of flag is either
- the flag of the country if it is loaded in the library
OR
- a demo flag of the globe (This provides a fall-back and help your app not crash due to nullPointerException)
*/
  • You can hard-code the country flag if you know the alpha2 code of the country. Eg. to set the flag of Sweden, you could do
<ImageView android:id="@+id/flagImageId" 
    android:layout_width="@dimens/imageWidth"
    android:layout_height="@dimens/imageHeight"
    android:src="@drawable/se"/> <!-- Sets this image to the Swedish flag -->
  • In java code, you could statically do same as
// Set the image of an imageView
final ImageView swedishFlag= (ImageView) findViewById(R.id.flagImageView);
swedishFlag.setImageResource(R.drawable.se);
  1. Get a Country with attributes like "id":4,"name":"Afghanistan","alpha2":"af","alpha3":"afg", flag:imageResource"
final Country afghanistan = World.getCountryFrom("af|afg|afghanistan|4");
// Log.d(TAG, afghanistan.toString()); 
  1. Get a list of all the countries with their identifiers
final List<Country> countries = World.getAllCountries();
// This list cannot be modified but you can get its contents
  1. Get list of countries from a continent
final List<Country> africanCounties = World.getCountriesFrom(Continent.AFRICA); 
///final List<Country> filteredCountries = World.getCountriesFrom(Continent.[AFRICA|ASIA|EUROPE|OCEANA|SOUTH_AMERICA|NORTH_AMERICA])
// Continent is an enum that has all the continents of the world

Link to javadoc --> javadoc link

All the steps above are demonstrated in this project --> world country flag demo


Demonstrating dynamic retrieval of country flags
Live retrieval of Country data

Get this sample app in the playstore Country Data Demo at playstore

Data sources for the project

All country flags

Most of the flags came from flagpedia.net. This site does not contain all the countries in the world so some where downloaded from wikipedia after quering the country name

Countries and their iso alpha values

All country names were download from Geonames using a Python project written by Bernard Longho aka @blongho. Check it out Countries data by blongho

Getting different dimensions of the flags

Some guys from Egypt made some awesome App icon generator which generates android drawables as well as iOS images(if you want) in different dimensions. It is super fast and can do batch processing of images.


Contribution guidelines

Please feel free to add more flags or modify any thing that would make this library more useful. The various ways of contribution are specified in CONTRIBUTING.md


Contact

Feel free to contact me to discuss anything related to development in particular and life in general.

Comments
  • sing-up apk not working with your lib - When we set  minifyEnabled true

    sing-up apk not working with your lib - When we set minifyEnabled true

    Thanks for you lib its grt work in debug mode app

    buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } }

    implementation 'com.google.android.material:material:1.1.0' implementation 'com.github.blongho:worldCountryData:v1.5'

            World.init(this);
    

    not getting flag = World.getFlagOf("CountryName");

    please help on that , I want to set minifyEnabled true when I create sign-up apk file

    question 
    opened by anilthummar 9
  • minsdk should be 21

    minsdk should be 21

    hi correct me if im wrong

    shouldn't min sdk be 21 instead of the current 15? there would be no point to have those vector images since it will still generate pngs.

    opened by chitgoks 6
  • Build fails on pipeline

    Build fails on pipeline

    The build on pipeline fails because of the below reason

    Failed to transform worldCountryData-1.5.2.jar (com.github.blongho:worldCountryData:1.5.2) to match attributes {artifactType=enumerated-runtime-classes, org.gradle.category=library, org.gradle.libraryelements=jar, org.gradle.status=release, org.gradle.usage=java-runtime}. > Could not find worldCountryData-1.5.2.jar (com.github.blongho:worldCountryData:1.5.2). Searched in the following locations: https://jitpack.io/com/github/blongho/worldCountryData/1.5.2/worldCountryData-1.5.2.jar

    opened by phomotech 5
  • Some countries doesn`t have flags

    Some countries doesn`t have flags

    Hello!

    I am using function: World.getFlagOf(item.CodeAlpha2)

    Works fine, but some countries and regions does not have flags:

    • Saint Maarten
    • Turkmenistan
    • Gibraltar
    • French Polynesia
    • New Caledonia
    • Wallis and Futuna
    • Indonesia
    • Guernsey
    • Ghana
    • Hong Kong
    • Macao

    Thank you.

    opened by Radcat501 3
  • Add new attribute

    Add new attribute "languages" to class "Country"

    I have modified the "Country" class to include another attribute "languages" which is a string to hold the spoken languages of the a country.

    opened by fatonhoti 3
  • Make classes

    Make classes "Country" and "Currency" Parcelable and fix spelling error in *Continent* Enum

    When trying to send for example a List<Country> from one activity to another via Intents, my application was crashing due to the fact that the class Country was not Parcelable. This pull request is my attempt to make the classes Country and Currency (Currency needs to be Parcelable too otherwise Country can't write Currency objects to it's Parcel) Parcelable in order to allow for the described functionality to work without crashing.

    Furthermore I noticed a spelling error in the enum regarding the continents, so I fixed that.

    opened by fatonhoti 3
  • Make class

    Make class "Country" Serializable

    The class "Country" is currently not Serializable which causes issues when trying to pass for example an "ArrayList<Country>" from one activity to another. Making it implement Serializable should fix the issue.

    opened by fatonhoti 3
  • Currencies not being loaded because of validation

    Currencies not being loaded because of validation

    There's a logic that checks whether the given currency is valid

    private boolean isValid(final Currency currency) {
        return currency != null && currency.getSymbol() != null && currency.getName() != null
            && currency.getCode() != null;
      }
    

    Yet, there are currencies which have no symbol defined, like Hong Kong Dollar

    {
        "country": "HK",
        "name": "Hong Kong dollar",
        "code": "HKD"
     }
    

    This effectively excludes such currencies from being added to currencyMap field.

    private void loadCurrencies(Context context) {
        final String currencyArray = AssetsReader.readFromAssets(context,
            R.raw.com_blongho_country_data_currencies);
        Gson gson = new Gson();
        final Currency[] currencies = gson.fromJson(currencyArray, Currency[].class);
        for (final Currency currency : currencies) {
          if (isValid(currency)) {
            currencyMap.put(currency.getCountry().toLowerCase(), currency);
          }
        }
      }
    

    Would it make sense to mark the symbol field as @Nullable and remove isValid() check?

    no-issue-activity 
    opened by serhuz 3
  • Problem with implementation

    Problem with implementation

    Describe the bug Cannot implement the library. implementation 'com.github.blongho:worldCountryData:v1.5' implementation 'com.github.blongho:worldCountryData:1.5'

    Error

    Failed to resolve: com.github.blongho:worldCountryData:1.5
    Show in Project Structure dialog
    Affected Modules: app
    

    Android Studio 4.0

    opened by ghost 3
  • Failed to resolve issue

    Failed to resolve issue

    I am getting the ERROR: Failed to resolve

    Whether I am using io.github.blongho:worldCountryData:1.3 or

    implementation 'com.github.blongho:worldCountryData:1.2.0'

    Added the maven repo to the gradle dependency, cleaned, rebuild project, invalidate cache & restart, but just cant seem to get this working?! Can't seem to figure out how to solve this. Any help?

    bug please_upgrade 
    opened by RikNorakomi 3
  • worldCountryData-1.4.aar is missing from the Jitpack repository

    worldCountryData-1.4.aar is missing from the Jitpack repository

    Describe the bug The file worldCountryData-1.4.aar is missing from Jitpack.

    To Reproduce Try to build a project with a dependency on version 1.4

    implementation 'com.github.blongho:worldCountryData:1.4'
    

    It will result in the error message Failed to resolve: worldCountryData-1.4.

    Expected behavior Sucessful resolution of dependencies when using version 1.4.

    Additional context

    $ curl https://jitpack.io/com/github/blongho/worldCountryData/1.4/
    build.log
    worldCountryData-1.4-sources.jar
    worldCountryData-1.4-sources.jar.md5
    worldCountryData-1.4-sources.jar.sha1
    worldCountryData-1.4.pom
    worldCountryData-1.4.pom.md5
    worldCountryData-1.4.pom.sha1
    

    As you can see in the file listing above worldCountryData-1.4.aar is missing from the repository despite being mentioned in the build.log.

    please_upgrade 
    opened by lclutz 2
  • java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toLowerCase()' on a null object reference

    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toLowerCase()' on a null object reference

    Describe the bug I am getting an error that is NullPointerException. It is in WorldData.java file where in this library.

    To Reproduce

    1. just write World.init(applicationContext) in App file or an Activity file.

    Expected behavior A clear and concise description of what you expected to happen.

    Screenshots Screen Shot 2022-11-30 at 12 24 49

    Screen Shot 2022-11-30 at 12 24 59

    Additional context I hadn't been getting this error before but now I am getting. It is weird I also use this library in my another app and this library is working as well there.

    opened by mustafaberk1996 2
Releases(v1.5.4-alpha-1)
  • v1.5.4-alpha-1(Jul 20, 2022)

  • v1.5.4-alpha(Jun 7, 2022)

    What's Changed

    • Adds languages to country
    • Removes validation checks that led to exclusion of some countries.
    • Added keep file to solve shrink resource true crash by @md0092651 in https://github.com/blongho/worldCountryData/pull/39
    • Fix incorrect flag for country "Kosovo", add new identifier "capital" to Country and fix some typos and comments. by @fatonhoti in https://github.com/blongho/worldCountryData/pull/37

    New Contributors

    • @md0092651 made their first contribution in https://github.com/blongho/worldCountryData/pull/39

    Full Changelog: https://github.com/blongho/worldCountryData/compare/v1.5.3...v1.5.4-alpha

    **Demo signed release apk added!

    Source code(tar.gz)
    Source code(zip)
    worldcountrydata-demo.apk(11.82 MB)
  • v1.5.3(Apr 4, 2022)

    What's Changed

    • Fix currencies by @sal0max in https://github.com/blongho/worldCountryData/pull/31
    • Fixes https://github.com/blongho/worldCountryData/issues/28 by @sal0max in https://github.com/blongho/worldCountryData/pull/32
    • Update com_blongho_country_data_currencies.json by @akrami1997 in https://github.com/blongho/worldCountryData/pull/25
    • Make classes "Country" and "Currency" Parcelable and fix spelling error in Continent Enum by @fatonhoti in https://github.com/blongho/worldCountryData/pull/36

    New Contributors

    • @sal0max made their first contribution in https://github.com/blongho/worldCountryData/pull/31
    • @akrami1997 made their first contribution in https://github.com/blongho/worldCountryData/pull/25
    • @fatonhoti made their first contribution in https://github.com/blongho/worldCountryData/pull/36

    Full Changelog: https://github.com/blongho/worldCountryData/compare/v1.5.2...v1.5.3

    Source code(tar.gz)
    Source code(zip)
  • v1.5.1(Jan 14, 2021)

    • Country data accurate as of 2021-01-12 08:30 CET from Geonames
    • Updates documentation of the readme as well as info on pro-guard
    • Adds possibility to easily get countries from a continent
    • Clean up code to remove un-used and unnecessary classes
    • Introduces filters such that only countries and currencies with all attributes are included.
    • Includes unite tests
    Source code(tar.gz)
    Source code(zip)
  • v1.5(Apr 5, 2020)

    • The library is now smaller thanks to @eagskunst for converting the flag resources to vector drawables.
    • Adds EU flag which can be used for EUROs currency
    • Includes more instrumentation tests.

    All method calls remain the same. Just change the version number

    Source code(tar.gz)
    Source code(zip)
  • v1.4(Feb 29, 2020)

    After thorough testing of the 1.4-beta, the following was done before this release

    • Minor code refactoring
    • Addition of unit tests
    • Updates documentation
    Source code(tar.gz)
    Source code(zip)
  • v1.4-beta01(Nov 16, 2019)

  • 1.3(Sep 29, 2019)

  • 1.3-alpha01(Sep 4, 2019)

  • v1.3-alpha(Jul 26, 2019)

  • v1.2.0(May 13, 2019)

    Do you want to know the most populated country in the world or the smallest country of the world or you are planning on a vacation and you want to know the main city (capital city)?

    In this release, you are now able to do the following

    get the country population get the capital city of the country get the surface area of the country get the continent of a country ==> Check the java documentation for more https://blongho.github.io/world-country-data/ Report any bugs by creating an issue

    In the future release, it will be possible to get the cities (all major cities) of any country and more more...

    Source code(tar.gz)
    Source code(zip)
  • v1.2.0-beta(May 10, 2019)

    Do you want to know the most populated country in the world or the smallest country of the world or you are planning on a vacation and you want to know the main city (capital city)?

    In this release, you are now able to do the following

    • get the country population
    • get the capital city of the country
    • get the surface area of the country
    • get the continent of a country

    ==> Check the java documentation for more https://blongho.github.io/world-country-data/doc/

    This is a pre-release, it has not been thoroughly tested. Report any bugs by creating an issue

    In the future release, it will be possible to get the cities (all major cities) of any country and more more...

    Source code(tar.gz)
    Source code(zip)
  • v1.1.0(Mar 14, 2019)

    Changes made

    1. Changed library package from countryFlags to country_data. The latter better conveys the information contained in the library. The former had only country flags as extra information apart from the country identifiers. This version and upcoming versions will have more information about a country.
    2. Cleaned code base and improve documentation

    The current version have the currency for each country. Subsequent versions will have more country data

    Source code(tar.gz)
    Source code(zip)
  • v1.1-beta(Mar 11, 2019)

    In this version, it is now possible to get the list of currencies of the world

    World.init(getApplicationContext()); // initialize data
    List<Currency> currencies = World.getAllCurrencies()
    // Currency [country=NZ, name=New Zealand Dollars, code=NZD, symbol=$]
    
    Source code(tar.gz)
    Source code(zip)
  • v1.1-alpha(Mar 8, 2019)

    In this release, one a Country also has a Currency

    Get the currency from a country by calling

    World.init(getApplicationContext());
    Country cameroon = World.getCountryFrom("cm");
    Currency curr = cameroon.getCurrency();
    
    Source code(tar.gz)
    Source code(zip)
  • v1.0(Mar 5, 2019)

    This release is provides a more intuitive interface for the user.

    Major changes include:

    1. Removal of ability of user to create a country in their app. In real world, countries are not created like that.

    2.To get a country, a user has to know any of the four identifiers of the country

    • The country name eg Sweden
    • The country numeric code eg 752
    • The alpha2 of the country eg "se"
    • The alpha3 of the country eg "swe"

    With these, Sweden can be created alongside its flag using

    World.init(getApplicationContext()); 
    // you call this just once in your program. It wraps a singleton 
    // that initializes everything that the library is to offer
    Country sweden = World.getCountryFrom("sweden"|"se"|"swe"|752); 
    
    1. To get the flag of a country, the user needs any of the identifiers mensioned in 2 above, then using two methods
    World.init(getApplicationContext());
    final int swedishFlag = World.getFlagOf("sweden"|"se"|"swe"|752);
    
    1. If you are interested in all the countries of the world
    World.init(getApplicationContext());
    final List<Country> countries = World.getAllCountries(); 
    // This list cannot be modified by adding, sorting or removing an element. 
    // You can only retrieve information therein
    ```¨
    NB: All Country identifiers have getters so you can use that to retrieve a Country
    from `List<Country> countries`
    

    Give a star to the repository to get updates of new features.

    Source code(tar.gz)
    Source code(zip)
  • v0.1.1(Feb 28, 2019)

    In this release, you can now get all the countries of the world and their flags by calling

    CountryFlag.getInstance(getApplicationContext()); // loads the countries and flags
    
    final List<Country> countryList = CountryFlag.allCountriesAndFlags(); // all country list
    

    Enjoy

    Source code(tar.gz)
    Source code(zip)
  • v0.1.0(Feb 27, 2019)

  • v0.1.0-beta(Feb 27, 2019)

Owner
Bernard Longho
Using programming to make the world a better place.
Bernard Longho
This is the kotlin-based android application. In this you can solve upto 100 flag-quiz question and enhance your skill.

Flag Quiz App - DOWNLOAD APP Enhance your knowledge by taking part in this Quiz - 100 Questions in 30 minutes It is based on the Kotlin language. You

Suryansh Prajapati 5 Dec 19, 2022
Movo (Movie Information) is an android application to find out all the Movie Information and Details.

Movo About The Project Screen.Recording.2022-08-12.at.08.53.46_1.mp4 Movo (Movie Information) is an android application to find out all the Movie Info

Reihan Fatilla 4 Sep 28, 2022
A news application through which you can learn and browse all the news that interests you by choosing the country and type of news with the ability to browse and add some news to your favorites

MY-NEWS-Android A news application through which you can learn and browse all the news that interests you by choosing the country and type of news wit

Mahmoud ELramady 0 Nov 11, 2021
Open source Crypto Currency Tracker Android App made fully in Kotlin

CoinBit CoinBit is a beautiful CryptoCurrency app, completely open sourced and 100% in kotlin. It supports following features Track prices of over 300

Pranay Airan 50 Dec 5, 2022
Android currency conversion app

Specie Android currency conversion. The app is available on F-Droid and here. This app is called Specie because I already have an app called Currency.

Bill Farmer 8 Oct 23, 2022
A simple currency converter app built With Jetpack Compose

I created this app to teach myself Architecture, Network Requests (using ktor), State Flow and other concurrency stuff.

Hardik Sachan 6 Jul 1, 2022
Covid Tracker - Show Details of Corona virus cases of all affected country

Covid_Tracker Based on MVVM Architecture Show Details of Corona virus cases of a

inderjeet yadav 1 Feb 27, 2022
A News Application Shows Breaking News of the Country with a feature to save News for future Use.

A News Application Shows Breaking News of the Country with a feature to save News for future Use.You can search news on any topic.Used all latest stuffs in android like Navigation Component, MVVM Architecture, Retrofit, Room DataBase, Kotlin Corutines etc

Aman Bhatt 2 Oct 20, 2022
Android app built with MVP architectural approach and uses Marvel Comics API that allows developers everywhere to access information about Marvel's vast library of comics. :zap:

Villains & Heroes Android app built with MVP architectural approach and uses Marvel Comics API that allows developers everywhere to access information

André Mion 53 Jul 13, 2022
An app that is a one-stop destination for all the CS enthusiasts, providing resources like Information scrapping techniques, best YT channels, courses available free-of-cost, etc. & knowledge about every domain and field that exists on the Internet related to Computer Science along with News, Jobs, and Internships opportunities in these domains along with valuable tips and hacks from mentors for a particular domain.

An app that is a one-stop destination for all the CS enthusiasts, providing resources like Information scrapping techniques, best YT channels, courses available free-of-cost, etc. & knowledge about every domain and field that exists on the Internet related to Computer Science along with News, Jobs, and Internships opportunities in these domains along with valuable tips and hacks from mentors for a particular domain.

CSwala 48 Nov 26, 2022
Display's information about SpaceX crew members and ships by consuming a rest api and storing the data to display when the user is offline.

Space-X App Display's information about SpaceX crew members(look for ‘Crew’ section in rest api docs) and ships (look for ‘Ships’ section in rest api

krishna chaitanya 2 Apr 8, 2022
An android app that lists all planets in our solar system and brings some information about them.

SolarSystem This application was developed in Android Studio and uses Kotlin as programming language. In short, it is an app that lists all the planet

Dayon Oliveira 0 Nov 3, 2021
An Android app that pulls the credit score information from a given endpoint and displays the records as a donut view

ClearScoreTest This is an Android app that pulls the credit score information fr

Abayomi Akanji 1 Jan 13, 2022
Android application with the announcements board of the Information and Electronic Engineering department of International Hellenic University.

Android application with the announcements board of the Information and Electronic Engineering department of International Hellenic University.

Raf 6 Jul 29, 2022
The News App has been carried out within the framework of the MVVM architecture, information about news is obtained by consulting an API, it is built usisng Jetpack Copose, Coroutines, Dependency Injection with Hilt and Retrofit

Journalist The News App consists of an application that displays the latest news from EEUU from an API that provides official and updated information.

null 0 Nov 3, 2021
Google map location tracker uploading current to realtime database and displaying location information from firebase realtime.

WEEK-8-SQ009-ANDROID LOCATION - GROUP WORK (2) Problem Description Track your partner(s). Implementation 1: You are to build a map application to show

null 0 Mar 16, 2022
Happy-Birthday - Design and implement a single screen app that displays information

Happy Birthday Android App | Android Basics in Kotlin Course Solution code for t

Anas Tariq 1 Feb 6, 2022
A mobile application that contains up-to-date information about the latest earthquakes in Turkey, scientific explanations about earthquakes, and Turkey's earthquake map.

Recent-Earthquakes A mobile application that contains up-to-date information about the latest earthquakes in Turkey, scientific explanations about ear

Nisa Efendioğlu 3 Dec 5, 2022