Android Get Country Emoji Flag Using Language

I saw that with Lollipop , Android has built-in Emoji flags for different countries. Can I use the device locale to get the Emoji flag for this country?

I wanted to insert an Emoji flag in a TextView that contains the user's location.

+11
android textview unicode emoji
source share
5 answers

I was looking for this too, but I don't think it is possible.

Have a look here: http://developer.android.com/reference/java/util/Locale.html

No mention of flags.

_

Alternatively you can check the answer here:

List of Android countries with flags and the availability of receiving iso mobile codes

which can help you.

0
source share

Emoji are Unicode characters. Based on the Unicode character table, Emoji flags consist of 26 Unicode (AZ) alphabetic characters for encoding ISO 3166-1 alpha-2 two-letter codes ( wiki ).

This means that you can split the two-letter country code and convert each letter AZ to the regional letter of the indicator symbol:

 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)); } 

Where 0x41 is the uppercase letter A , and 0x1F1E6 is the REGIONAL INDICATOR SYMBOL LETTER A in the Unicode table.

Note. This sample code is simplified and does not require country code checks that may not be available within the locale.

+29
source share

Based on this answer , I wrote a version of Kotlin below using the extension function.

I also added some checks to handle the unknown country code.

 /** * This method is to change the country code like "us" into πŸ‡ΊπŸ‡Έ * Stolen from /questions/826446/android-get-country-emoji-flag-using-locale/3009217#3009217 * 1. It first checks if the string consists of only 2 characters: ISO 3166-1 alpha-2 two-letter country codes (https://en.wikipedia.org/wiki/Regional_Indicator_Symbol). * 2. It then checks if both characters are alphabet * do nothing if it does not fulfil the 2 checks * caveat: if you enter an invalid 2 letter country code, say "XX", it will pass the 2 checks, and it will return unknown result */ fun String.toFlagEmoji(): String { // 1. It first checks if the string consists of only 2 characters: ISO 3166-1 alpha-2 two-letter country codes (https://en.wikipedia.org/wiki/Regional_Indicator_Symbol). if (this.length != 2) { return this } val countryCodeCaps = this.toUpperCase() // upper case is important because we are calculating offset val firstLetter = Character.codePointAt(countryCodeCaps, 0) - 0x41 + 0x1F1E6 val secondLetter = Character.codePointAt(countryCodeCaps, 1) - 0x41 + 0x1F1E6 // 2. It then checks if both characters are alphabet if (!countryCodeCaps[0].isLetter() || !countryCodeCaps[1].isLetter()) { return this } return String(Character.toChars(firstLetter)) + String(Character.toChars(secondLetter)) } 

Code snippet

I also included a working Kotlin snippet using the Kotlin Playground. To start a fragment, you must:

  1. click "Show code snippet"
  2. click "Run snippet of code"
  3. press the play button in the upper right part of the generated console
  4. scroll down to see the result (it is hidden ..)
     <script src="https://unpkg.com/ kotlin-playground@1.6.0 /dist/playground.min.js" data-selector=".code"></script> <div class="code" style="display:none;"> /** * This method is to change the country code like "us" into πŸ‡ΊπŸ‡Έ * Stolen from /questions/826446/android-get-country-emoji-flag-using-locale/3009217#3009217 * 1. It first checks if the string consists of only 2 characters: ISO 3166-1 alpha-2 two-letter country codes (https://en.wikipedia.org/wiki/Regional_Indicator_Symbol). * 2. It then checks if both characters are alphabet * do nothing if it does not fulfil the 2 checks * caveat: if you enter an invalid 2 letter country code, say "XX", it will pass the 2 checks, and it will return unknown result */ fun String.toFlagEmoji(): String { // 1. It first checks if the string consists of only 2 characters: ISO 3166-1 alpha-2 two-letter country codes (https://en.wikipedia.org/wiki/Regional_Indicator_Symbol). if (this.length != 2) { return this } val countryCodeCaps = this.toUpperCase() // upper case is important because we are calculating offset val firstLetter = Character.codePointAt(countryCodeCaps, 0) - 0x41 + 0x1F1E6 val secondLetter = Character.codePointAt(countryCodeCaps, 1) - 0x41 + 0x1F1E6 // 2. It then checks if both characters are alphabet if (!countryCodeCaps[0].isLetter() || !countryCodeCaps[1].isLetter()) { return this } return String(Character.toChars(firstLetter)) + String(Character.toChars(secondLetter)) } fun main(args: Array&lt;String&gt;){ println("us".toFlagEmoji()) println("AF".toFlagEmoji()) println("BR".toFlagEmoji()) println("MY".toFlagEmoji()) println("JP".toFlagEmoji()) } </div> 
+8
source share

When I first wrote this answer, for some reason I did not notice that I only worked on Android through React Native!

Anyway, here is my JavaScript solution that works with or without ES6 support.

  function countryCodeToFlagEmoji(country) { return typeof String.fromCodePoint === "function" ? String.fromCodePoint(...[...country].map(c => c.charCodeAt() + 0x1f185)) : [...country] .map(c => "\ud83c" + String.fromCharCode(0xdd85 + c.charCodeAt())) .join(""); } console.log(countryCodeToFlagEmoji("au")); console.log(countryCodeToFlagEmoji("aubdusca")); 

If you want to list in country codes instead of capital letters, just change the two offsets to 0x1f1a5 and 0xdda5 .

+1
source share

I use it so easily. Get Unicode from here .

For the flag of Bangladesh it is U+1F1E7 U+1F1E9 Now

 {... String flag = getEmojiByUnicode(0x1F1E7)+getEmojiByUnicode(0x1F1E9)+ " Bangladesh"; } public String getEmojiByUnicode(int unicode){ return new String(Character.toChars(unicode)); } 

It will show> (Bangladesh flag) Bangladesh

+1
source share

All Articles