Last active
July 26, 2024 21:22
-
-
Save BurakDizlek/9eb54a0245a765dbdd312148f1c96d7f to your computer and use it in GitHub Desktop.
Get to flag unicode as String and use it anywhere.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CountryFlags { | |
private static String A = getEmojiByUnicode(0x1F1E6); | |
private static String B = getEmojiByUnicode(0x1F1E7); | |
private static String C = getEmojiByUnicode(0x1F1E8); | |
private static String D = getEmojiByUnicode(0x1F1E9); | |
private static String E = getEmojiByUnicode(0x1F1EA); | |
private static String F = getEmojiByUnicode(0x1F1EB); | |
private static String G = getEmojiByUnicode(0x1F1EC); | |
private static String H = getEmojiByUnicode(0x1F1ED); | |
private static String I = getEmojiByUnicode(0x1F1EE); | |
private static String J = getEmojiByUnicode(0x1F1EF); | |
private static String K = getEmojiByUnicode(0x1F1F0); | |
private static String L = getEmojiByUnicode(0x1F1F1); | |
private static String M = getEmojiByUnicode(0x1F1F2); | |
private static String N = getEmojiByUnicode(0x1F1F3); | |
private static String O = getEmojiByUnicode(0x1F1F4); | |
private static String P = getEmojiByUnicode(0x1F1F5); | |
private static String Q = getEmojiByUnicode(0x1F1F6); | |
private static String R = getEmojiByUnicode(0x1F1F7); | |
private static String S = getEmojiByUnicode(0x1F1F8); | |
private static String T = getEmojiByUnicode(0x1F1F9); | |
private static String U = getEmojiByUnicode(0x1F1FA); | |
private static String V = getEmojiByUnicode(0x1F1FB); | |
private static String W = getEmojiByUnicode(0x1F1FC); | |
private static String X = getEmojiByUnicode(0x1F1FD); | |
private static String Y = getEmojiByUnicode(0x1F1FE); | |
private static String Z = getEmojiByUnicode(0x1F1FF); | |
private static String getCodeByCharacter(Character character) { | |
String code; | |
switch (Character.toUpperCase(character)) { | |
case 'A': | |
code = A; | |
break; | |
case 'B': | |
code = B; | |
break; | |
case 'C': | |
code = C; | |
break; | |
case 'D': | |
code = D; | |
break; | |
case 'E': | |
code = E; | |
break; | |
case 'F': | |
code = F; | |
break; | |
case 'G': | |
code = G; | |
break; | |
case 'H': | |
code = H; | |
break; | |
case 'I': | |
code = I; | |
break; | |
case 'J': | |
code = J; | |
break; | |
case 'K': | |
code = K; | |
break; | |
case 'L': | |
code = L; | |
break; | |
case 'M': | |
code = M; | |
break; | |
case 'N': | |
code = N; | |
break; | |
case 'O': | |
code = O; | |
break; | |
case 'P': | |
code = P; | |
break; | |
case 'Q': | |
code = Q; | |
break; | |
case 'R': | |
code = R; | |
break; | |
case 'S': | |
code = S; | |
break; | |
case 'T': | |
code = T; | |
break; | |
case 'U': | |
code = U; | |
break; | |
case 'V': | |
code = V; | |
break; | |
case 'W': | |
code = W; | |
break; | |
case 'X': | |
code = X; | |
break; | |
case 'Y': | |
code = Y; | |
break; | |
case 'Z': | |
code = Z; | |
break; | |
default: | |
code = ""; | |
break; | |
} | |
return code; | |
} | |
private static String getEmojiByUnicode(int unicode) { | |
return new String(Character.toChars(unicode)); | |
} | |
public static String getCountryFlagByCountryCode(String countryCode) { | |
String flag; | |
if (countryCode.length() == 2) { | |
flag = getCodeByCharacter(countryCode.charAt(0)) + getCodeByCharacter(countryCode.charAt(1)); | |
} else { | |
flag = ""; | |
} | |
return flag; | |
} | |
} |
The 'X' character is missing so flags for countries like Mexico do not work.
Need to add:
private static String X = getEmojiByUnicode(0x1F1FD);
to the initial String definitions and:
case 'X':
code = X;
break;
to the switch in getCodeByCharacter()
.
Thank you @TheFatMonk . I updated :)
And here the Kotlin version:
https://gist.github.com/cyb3rko/3aeadc9efd191330601ed1939810cf7c
Thanks for this gist!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example usage :
String countryWithFlag = CountryFlags.getCountryFlagByCountryCode("TR") + " " + "Türkiye";
Output : 🇹🇷 Türkiye
You can use it with Android TextView too :)
Note : It is working very well Android 6 and above.