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.
Known Limitations
This library has been known to crash apps if you set shrinkResources true
in your build settings. See step 2.3 below on how to deal with this!
This library will also result to a larger APK size of your app! It however gives you the advantage that it does not require any network calls
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
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
}
compileOptions {
sourceCompatibility 1.8
targetCompatibility 1.8
}
...
}
Build your project (and make sure gradle has successfully synced)
Buid >> Clean Project, Build >> Rebuild Project
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
// 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)
*/
<ImageView android:id="@+id/flagImageId" android:layout_width="@dimens/imageWidth"
android:layout_height="@dimens/imageHeight"
android:src="https://github.com/blongho/worldCountryData/raw/master/@drawable/se" /> <!-- Sets this image to the Swedish flag -->
// Set the image of an imageView
final ImageView swedishFlag=(ImageView)findViewById(R.id.flagImageView);
swedishFlag.setImageResource(R.drawable.se);
"id":4,"name":"Afghanistan","alpha2":"af","alpha3":"afg", flag:imageResource"
final Country afghanistan=World.getCountryFrom("af|afg|afghanistan|4|kabul");
// Log.d(TAG, afghanistan.toString());
final List<Country> countries=World.getAllCountries();
// This list cannot be modified but you can get its contents
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
NEW
final List<String> languages=World.getLanguagesFrom("af|afg|afghanistan|4|kabul");
//or
final Country afghanistan=World.getCountryFrom("af|afg|afghanistan|4|kabul");
final List<String> languages=afghanistan.getLanguages();
// Returns comma separated list of country languages e.g [Swedish (sv-SE), Northern Sami (se), Southern Sami (sma), Finnish (fi-SE)]
Link to javadoc --> javadoc link
All the steps above are demonstrated in this project --> world country flag demo
| | |:--:| | Live retrieval of Country data |
Get this sample app in the playstore
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
Feel free to contact me to discuss anything related to development in particular and life in general.