Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Get Country Emoji Flag Using Locale

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.

like image 750
Mike Walker Avatar asked May 27 '15 22:05

Mike Walker


People also ask

How do I get a country flag on my Android?

You can get the flag of a country by using the two iso alpha2 or alpha3 or the country name or the numeric code.

What is this country flag 🏴?

🏴󠁧󠁢󠁥󠁮󠁧󠁿 Flag: England.

Do Androids have flag Emojis?

Android Lollipop adds 209 new emoji icons, but they're all flags.

Is there a country emoji?

🎌 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.

What countries use emojis as flags?

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.

How to get the Android locale country code?

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)

What is the Unicode code for the flag emoji?

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.

What are the two-letter country codes in Unicode?

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.


1 Answers

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.

like image 63
Dmitry Avatar answered Sep 23 '22 01:09

Dmitry