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.
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.
Based on this answer, I wrote a Kotlin version below using extension function.
I also added some checks to handle unknown country code.
/**
* This method is to change the country code like "us" into 🇺🇸
* Stolen from https://stackoverflow.com/a/35849652/75579
* 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 doesn't 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))
}
Runnable Code Snippet
I also included a runnable Kotlin snippet using Kotlin Playground. In order to run the snippet you need to:
click "Show code snippet"
click "Run Code Snippet"
click the play button at the right top of the generated console
scroll to the bottom to see the result (it's 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 https://stackoverflow.com/a/35849652/75579
* 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 doesn't 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<String>){
println("us".toFlagEmoji())
println("AF".toFlagEmoji())
println("BR".toFlagEmoji())
println("MY".toFlagEmoji())
println("JP".toFlagEmoji())
}
</div>
When I first wrote this answer I somehow overlooked that I've only worked on Android via React Native!
Anyway, here's 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 pass in the country codes as capital letters instead, just change the two offsets to 0x1f1a5 and 0xdda5.
You can get the country code very simple.
I want to talk about flag selection according to country code.
I wrote a class about it and it is very simple to use.
usage:
String countryWithFlag = CountryFlags.getCountryFlagByCountryCode("TR") + " " + "Türkiye";
Output : 🇹🇷 Türkiye
You can use it with Android TextView too :)
You can check out the class here
It works very well on Android 6 and above.
I am using this so easily.
Get the Unicode from here.
For Bangladesh flag 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 > (Bangladeshi flag) Bangladesh
I was looking for that too but I don't think it's possible yet.
Have a look here:
http://developer.android.com/reference/java/util/Locale.html
No mentioning about flags.
_
Alternately you can check the answer here:
Android Countries list with flags and availability of getting iso mobile codes
that might help you.
This question already has answers here:
Extract code country from phone number [libphonenumber]
(7 answers)
Closed 5 years ago.
I am using libphonenumber api https://code.google.com/p/libphonenumber/ in android project, I am getting country code using following code
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
try {
PhoneNumber numberProto = phoneUtil.parse(number, "IN");
int countryCode = numberProto.getCountryCode();
} catch (NumberParseException e) {
System.err.println("NumberParseException was thrown: " + e.toString());
}
My question is:-
-- I have number like +447870000000 but don't know about country code but want to get country code from this number like +44 how can I get it through this api ?
please help me
Thanks in advance
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.service_activity);
/*********** read indicative sim *************/
TelephonyManager telMgr = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
String simContryiso = telMgr.getSimCountryIso();
String indicative = Iso2Phone.getPhone(simContryiso);
}
//create a class which contains the indicative of all the country and the ISO code
public class Iso2Phone
{
// Get the indicative
public static String getPhone(String code)
{
return country_to_indicative.get(code.toUpperCase());
}
private static Map<String, String> country_to_indicative = new HashMap<String, String>();
static
{
country_to_indicative.put("AF", "+93");
country_to_indicative.put("AL", "+355");
country_to_indicative.put("DZ", "+213");
country_to_indicative.put("AS", "+1684");
country_to_indicative.put("AD", "+376");
country_to_indicative.put("AO", "+244");
country_to_indicative.put("AI", "+1264");
country_to_indicative.put("AG", "+1268");
country_to_indicative.put("AR", "+54");
country_to_indicative.put("AM", "+374");
country_to_indicative.put("AU", "+61");
country_to_indicative.put("AW", "+297");
country_to_indicative.put("AT", "+43");
country_to_indicative.put("AZ", "+994");
country_to_indicative.put("BS", "+1242");
country_to_indicative.put("BH", "+973");
country_to_indicative.put("BD", "+880");
country_to_indicative.put("BB", "+1246");
country_to_indicative.put("BY", "+375");
country_to_indicative.put("BE", "+32");
country_to_indicative.put("BZ", "+501");
country_to_indicative.put("BJ", "+229");
country_to_indicative.put("BM", "+1441");
country_to_indicative.put("BT", "+975");
country_to_indicative.put("BO", "+591");
country_to_indicative.put("BA", "+387");
country_to_indicative.put("BW", "+267");
country_to_indicative.put("BR", "+55");
country_to_indicative.put("BN", "+673");
country_to_indicative.put("BG", "+359");
country_to_indicative.put("BF", "+226");
country_to_indicative.put("BI", "+257");
country_to_indicative.put("KH", "+855");
country_to_indicative.put("CM", "+237");
country_to_indicative.put("CA", "+1");
country_to_indicative.put("CV", "+238");
country_to_indicative.put("CF", "+236");
country_to_indicative.put("TD", "+235");
country_to_indicative.put("CL", "+56");
country_to_indicative.put("CN", "+86");
country_to_indicative.put("CO", "+57");
country_to_indicative.put("KM", "+269");
country_to_indicative.put("CD", "+243");
country_to_indicative.put("CG", "+242");
country_to_indicative.put("CR", "+506");
country_to_indicative.put("CI", "+225");
country_to_indicative.put("HR", "+385");
country_to_indicative.put("CU", "+53");
country_to_indicative.put("CY", "+357");
country_to_indicative.put("CZ", "+420");
country_to_indicative.put("DK", "+45");
country_to_indicative.put("DJ", "+253");
country_to_indicative.put("DM", "+1767");
country_to_indicative.put("DO", "+1829");
country_to_indicative.put("EC", "+593");
country_to_indicative.put("EG", "+20");
country_to_indicative.put("SV", "+503");
country_to_indicative.put("GQ", "+240");
country_to_indicative.put("ER", "+291");
country_to_indicative.put("EE", "+372");
country_to_indicative.put("ET", "+251");
country_to_indicative.put("FJ", "+679");
country_to_indicative.put("FI", "+358");
country_to_indicative.put("FR", "+33");
country_to_indicative.put("GA", "+241");
country_to_indicative.put("GM", "+220");
country_to_indicative.put("GE", "+995");
country_to_indicative.put("DE", "+49");
country_to_indicative.put("GH", "+233");
country_to_indicative.put("GR", "+30");
country_to_indicative.put("GD", "+1473");
country_to_indicative.put("GT", "+502");
country_to_indicative.put("GN", "+224");
country_to_indicative.put("GW", "+245");
country_to_indicative.put("GY", "+592");
country_to_indicative.put("HT", "+509");
country_to_indicative.put("HN", "+504");
country_to_indicative.put("HU", "+36");
country_to_indicative.put("IS", "+354");
country_to_indicative.put("IN", "+91");
country_to_indicative.put("ID", "+62");
country_to_indicative.put("IR", "+98");
country_to_indicative.put("IQ", "+964");
country_to_indicative.put("IE", "+353");
country_to_indicative.put("IL", "+972");
country_to_indicative.put("IT", "+39");
country_to_indicative.put("JM", "+1876");
country_to_indicative.put("JP", "+81");
country_to_indicative.put("JO", "+962");
country_to_indicative.put("KZ", "+7");
country_to_indicative.put("KE", "+254");
country_to_indicative.put("KI", "+686");
country_to_indicative.put("KP", "+850");
country_to_indicative.put("KR", "+82");
country_to_indicative.put("KW", "+965");
country_to_indicative.put("KG", "+996");
country_to_indicative.put("LA", "+856");
country_to_indicative.put("LV", "+371");
country_to_indicative.put("LB", "+961");
country_to_indicative.put("LS", "+266");
country_to_indicative.put("LR", "+231");
country_to_indicative.put("LY", "+218");
country_to_indicative.put("LI", "+423");
country_to_indicative.put("LT", "+370");
country_to_indicative.put("LU", "+352");
country_to_indicative.put("MK", "+389");
country_to_indicative.put("MG", "+261");
country_to_indicative.put("MW", "+265");
country_to_indicative.put("MY", "+60");
country_to_indicative.put("MV", "+960");
country_to_indicative.put("ML", "+223");
country_to_indicative.put("MT", "+356");
country_to_indicative.put("MH", "+692");
country_to_indicative.put("MR", "+222");
country_to_indicative.put("MU", "+230");
country_to_indicative.put("MX", "+52");
country_to_indicative.put("FM", "+691");
country_to_indicative.put("MD", "+373");
country_to_indicative.put("MC", "+377");
country_to_indicative.put("MN", "+976");
country_to_indicative.put("ME", "+382");
country_to_indicative.put("MA", "+212");
country_to_indicative.put("MZ", "+258");
country_to_indicative.put("MM", "+95");
country_to_indicative.put("NA", "+264");
country_to_indicative.put("NR", "+674");
country_to_indicative.put("NP", "+977");
country_to_indicative.put("NL", "+31");
country_to_indicative.put("NZ", "+64");
country_to_indicative.put("NI", "+505");
country_to_indicative.put("NE", "+227");
country_to_indicative.put("NG", "+234");
country_to_indicative.put("NO", "+47");
country_to_indicative.put("OM", "+968");
country_to_indicative.put("PK", "+92");
country_to_indicative.put("PW", "+680");
country_to_indicative.put("PA", "+507");
country_to_indicative.put("PG", "+675");
country_to_indicative.put("PY", "+595");
country_to_indicative.put("PE", "+51");
country_to_indicative.put("PH", "+63");
country_to_indicative.put("PL", "+48");
country_to_indicative.put("PT", "+351");
country_to_indicative.put("QA", "+974");
country_to_indicative.put("RO", "+40");
country_to_indicative.put("RU", "+7");
country_to_indicative.put("RW", "+250");
country_to_indicative.put("KN", "+1869");
country_to_indicative.put("LC", "+1758");
country_to_indicative.put("VC", "+1784");
country_to_indicative.put("WS", "+685");
country_to_indicative.put("SM", "+378");
country_to_indicative.put("ST", "+239");
country_to_indicative.put("SA", "+966");
country_to_indicative.put("SN", "+221");
country_to_indicative.put("RS", "+381");
country_to_indicative.put("SC", "+248");
country_to_indicative.put("SL", "+232");
country_to_indicative.put("SG", "+65");
country_to_indicative.put("SK", "+421");
country_to_indicative.put("SI", "+386");
country_to_indicative.put("SB", "+677");
country_to_indicative.put("SO", "+252");
country_to_indicative.put("ZA", "+27");
country_to_indicative.put("ES", "+34");
country_to_indicative.put("LK", "+94");
country_to_indicative.put("SD", "+249");
country_to_indicative.put("SR", "+597");
country_to_indicative.put("SZ", "+268");
country_to_indicative.put("SE", "+46");
country_to_indicative.put("CH", "+41");
country_to_indicative.put("SY", "+963");
country_to_indicative.put("TJ", "+992");
country_to_indicative.put("TZ", "+255");
country_to_indicative.put("TH", "+66");
country_to_indicative.put("TL", "+670");
country_to_indicative.put("TG", "+228");
country_to_indicative.put("TO", "+676");
country_to_indicative.put("TT", "+1868");
country_to_indicative.put("TN", "+216");
country_to_indicative.put("TR", "+90");
country_to_indicative.put("TM", "+993");
country_to_indicative.put("TV", "+688");
country_to_indicative.put("UG", "+256");
country_to_indicative.put("UA", "+380");
country_to_indicative.put("AE", "+971");
country_to_indicative.put("GB", "+44");
country_to_indicative.put("US", "+1");
country_to_indicative.put("UY", "+598");
country_to_indicative.put("UZ", "+998");
country_to_indicative.put("VU", "+678");
country_to_indicative.put("VA", "+39");
country_to_indicative.put("VE", "+58");
country_to_indicative.put("VN", "+84");
country_to_indicative.put("YE", "+967");
country_to_indicative.put("ZM", "+260");
country_to_indicative.put("ZW", "+263");
country_to_indicative.put("GE", "+995");
country_to_indicative.put("TW", "+886");
country_to_indicative.put("AZ", "+994");
country_to_indicative.put("MD", "+373");
country_to_indicative.put("SO", "+252");
country_to_indicative.put("GE", "+995");
country_to_indicative.put("AU", "+61");
country_to_indicative.put("CX", "+61");
country_to_indicative.put("CC", "+61");
country_to_indicative.put("NF", "+672");
country_to_indicative.put("NC", "+687");
country_to_indicative.put("PF", "+689");
country_to_indicative.put("YT", "+262");
country_to_indicative.put("GP", "+590");
country_to_indicative.put("GP", "+590");
country_to_indicative.put("PM", "+508");
country_to_indicative.put("WF", "+681");
country_to_indicative.put("PF", "+689");
country_to_indicative.put("CK", "+682");
country_to_indicative.put("NU", "+683");
country_to_indicative.put("TK", "+690");
country_to_indicative.put("GG", "+44");
country_to_indicative.put("IM", "+44");
country_to_indicative.put("JE", "+44");
country_to_indicative.put("AI", "+1264");
country_to_indicative.put("BM", "+1441");
country_to_indicative.put("IO", "+246");
country_to_indicative.put("VG", "+1284");
country_to_indicative.put("KY", "+1345");
country_to_indicative.put("FK", "+500");
country_to_indicative.put("GI", "+350");
country_to_indicative.put("MS", "+1664");
country_to_indicative.put("PN", "+870");
country_to_indicative.put("SH", "+290");
country_to_indicative.put("TC", "+1649");
country_to_indicative.put("MP", "+1670");
country_to_indicative.put("PR", "+1");
country_to_indicative.put("AS", "+1684");
country_to_indicative.put("GU", "+1671");
country_to_indicative.put("VI", "+1340");
country_to_indicative.put("HK", "+852");
country_to_indicative.put("MO", "+853");
country_to_indicative.put("FO", "+298");
country_to_indicative.put("GL", "+299");
country_to_indicative.put("GF", "+594");
country_to_indicative.put("GP", "+590");
country_to_indicative.put("MQ", "+596");
country_to_indicative.put("RE", "+262");
country_to_indicative.put("AX", "+35818");
country_to_indicative.put("AW", "+297");
country_to_indicative.put("AN", "+599");
country_to_indicative.put("SJ", "+47");
country_to_indicative.put("AC", "+247");
country_to_indicative.put("TA", "+290");
country_to_indicative.put("AQ", "+6721");
country_to_indicative.put("CS", "+381");
country_to_indicative.put("PS", "+970");
country_to_indicative.put("EH", "+212");
}
}
A nice Library has been provided
https://github.com/hbb20/CountryCodePickerProject
In dependency add
dependencies {
implementation 'com.hbb20:ccp:2.2.0'
}
then use this xml code
<com.hbb20.CountryCodePicker
android:id="#+id/ccp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
fbutton:ccp_contentColor="#color/appcolor"
fbutton:ccp_textSize="#dimen/_10sdp" />
It will automatically populate the list of country code and also u can search in it.
If you have a number in a E164 format (in your case +447870000000) and you specify country code ISO in the parser, it will actually ignore the given iso code and do the formatting based on the number.
Country code ISO is needed when the number you give to the method is in national format.
As far as your code snippet goes, it should work.