I have seen that since Lollipop
, Android has built in Emoji
flags for different countries. Is it possible to use the devices locale to retrieve the Emoji
flag for that country?
I wanted to insert the Emoji
flag into a TextView
which contains the user's location.
You can get the flag of a country by using the two iso alpha2 or alpha3 or the country name or the numeric code.
🏴 Flag: England.
Android Lollipop adds 209 new emoji icons, but they're all flags.
🎌 Flags. List of country flag emojis. 🇯🇵 🇰🇷 🇩🇪 🇨🇳 🇺🇸 🇫🇷 🇪🇸 🇮🇹 🇷🇺 🇬🇧 Emoji flags are supported on all major platforms except Windows, which displays two-letter country codes instead of emoji flag images.
Emoji country flags are based on ISO 3166-1: a list of internationally recognized two-letter country codes. As of 2019 England, Scotland and Wales are the only RGI subdivision flags. All emoji names are official character and/or CLDR names and code points listed as part of the Unicode Standard.
Here you use locale.getLanguage () to get the Android Locale country code of the language that is currently being used in your Android device. 9- Build and run the app to see the progress. Getting Android Locale country code. ( Large preview)
unicode: Flag emoji Unicode character sequence, e.g. U+1F1EE U+1F1E9. Read more about Regional Indicator Symbol. name: The country name. emoji: The country flag emoji.
code: The two-letter country code in ISO 3166-1 alpha-2 format. unicode: Flag emoji Unicode character sequence, e.g. U+1F1EE U+1F1E9. Read more about Regional Indicator Symbol. name: The country name. emoji: The country flag emoji.
Emoji is a Unicode symbols. Based on the Unicode character table Emoji flags consist of 26 alphabetic Unicode characters (A-Z) intended to be used to encode ISO 3166-1 alpha-2 two-letter country codes (wiki).
That means it is possible to split two-letter country code and convert each A-Z letter to regional indicator symbol letter:
private String localeToEmoji(Locale locale) {
String countryCode = locale.getCountry();
int firstLetter = Character.codePointAt(countryCode, 0) - 0x41 + 0x1F1E6;
int secondLetter = Character.codePointAt(countryCode, 1) - 0x41 + 0x1F1E6;
return new String(Character.toChars(firstLetter)) + new String(Character.toChars(secondLetter));
}
Or in Kotlin, for example (assuming UTF-8):
val Locale.flagEmoji: String
get() {
val firstLetter = Character.codePointAt(country, 0) - 0x41 + 0x1F1E6
val secondLetter = Character.codePointAt(country, 1) - 0x41 + 0x1F1E6
return String(Character.toChars(firstLetter)) + String(Character.toChars(secondLetter))
}
Where 0x41
represents uppercase A
letter and 0x1F1E6
is REGIONAL INDICATOR SYMBOL LETTER A
in the Unicode table.
Note: This code example is simplified and doesn't have required checks related to country code, that could be not available inside the locale.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With