Reputation: 6249
I have a code very similar to the Uber clone app to show a country picker form. I understand that the flags are taken from flagResource = Resources.open("/flags.res");
, from the Codename One SMS Library
The problem is that a lot of flags are missing. A user may be sorry if the flag of his/her country is not there.
How can I insert all the flags?
Upvotes: 2
Views: 1564
Reputation: 6249
I found a solution to show all the flags. I'm going to share my code and exactly how I proceeded...
STEP 1 - HOW MANY COUNTRY CODES?
Complete List of Country & Dialing Codes (248 country codes) - https://www.worldatlas.com/aatlas/ctycodes.htm
I compared the previous list with these:
The first list with 248 country codes is probabily the one to be used. Note that 248 country codes were used also by Whatsapp (in the 2016), it's an hint of the right direction: https://www.quora.com/Whatsapp-has-248-flags-in-its-symbols-keyboard-in-its-latest-update-But-there-are-nearly-190-200-countries-on-earth-What-is-it-that-I-am-missing
However the current version of Whatsapp has 258 flags (I counted them extracting them from its apk). However, even if I suppose that the flags cannot fall within anyone's intellectual property, I think that it's safer to use flags from an open source repository, like the following one.
I'm going to use the 248 country codes list because it's the more suitable for our purposes (in my opinion, after several checkings).
STEP 2 - DOWNLOAD THE FLAGS
The 248 flags (in SVG format) can be downloaded from: https://github.com/joielechong/iso-country-flags-svg-collection
The interesting folder for our purposes is this one:
iso-country-flags-svg-collection-master/svg/country-4x3
It contains more flags than the 248 available country codes (they are 262 in total), but it's not a problem (the opposite should be a problem, of course...).
I converted all of them to PNGs files with the size of 150x112 pixels with the following command (on Linux with Inkscape installed):
for i in *.svg; do inkscape $i --export-width=150 --export-height=112 --export-png=`echo $i | sed -e 's/svg$/png/'`; done
You can download these generated PNGs from: http://jmp.sh/vXaA7eC
STEP 3 - CREATE NEW ARRAYS FOR THE APP
In the Uber clone, we have the following arrays, each of them contains 243 strings:
I replaced the import of these array from com.codename1.sms.activation.ActivationForm.*
with the following ones declared in my package (all of them contains 248 strings):
public static final String[] COUNTRY_NAMES = {"Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra", "Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina", "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia", "Bonaire", "Bosnia and Herzegovina", "Botswana", "Bouvet Island", "Brazil", "British Indian Ocean Territory", "Brunei Darussalam", "Bulgaria", "Burkina Faso", "Burundi", "Cambodia", "Cameroon", "Canada", "Cape Verde", "Cayman Islands", "Central African Republic", "Chad", "Chile", "China", "Christmas Island", "Cocos (Keeling) Islands", "Colombia", "Comoros", "Congo", "Democratic Republic of the Congo", "Cook Islands", "Costa Rica", "Croatia", "Cuba", "Curacao", "Cyprus", "Czech Republic", "Cote d'Ivoire", "Denmark", "Djibouti", "Dominica", "Dominican Republic", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia", "Falkland Islands (Malvinas)", "Faroe Islands", "Fiji", "Finland", "France", "French Guiana", "French Polynesia", "French Southern Territories", "Gabon", "Gambia", "Georgia", "Germany", "Ghana", "Gibraltar", "Greece", "Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala", "Guernsey", "Guinea", "Guinea-Bissau", "Guyana", "Haiti", "Heard Island and McDonald Islands", "Holy See (Vatican City State)", "Honduras", "Hong Kong", "Hungary", "Iceland", "India", "Indonesia", "Iran, Islamic Republic of", "Iraq", "Ireland", "Isle of Man", "Israel", "Italy", "Jamaica", "Japan", "Jersey", "Jordan", "Kazakhstan", "Kenya", "Kiribati", "Korea, Democratic People's Republic of", "Korea, Republic of", "Kuwait", "Kyrgyzstan", "Lao People's Democratic Republic", "Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein", "Lithuania", "Luxembourg", "Macao", "Macedonia, the Former Yugoslav Republic of", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands", "Martinique", "Mauritania", "Mauritius", "Mayotte", "Mexico", "Micronesia, Federated States of", "Moldova, Republic of", "Monaco", "Mongolia", "Montenegro", "Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia", "Nauru", "Nepal", "Netherlands", "New Caledonia", "New Zealand", "Nicaragua", "Niger", "Nigeria", "Niue", "Norfolk Island", "Northern Mariana Islands", "Norway", "Oman", "Pakistan", "Palau", "Palestine, State of", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines", "Pitcairn", "Poland", "Portugal", "Puerto Rico", "Qatar", "Romania", "Russian Federation", "Rwanda", "Reunion", "Saint Barthelemy", "Saint Helena", "Saint Kitts and Nevis", "Saint Lucia", "Saint Martin (French part)", "Saint Pierre and Miquelon", "Saint Vincent and the Grenadines", "Samoa", "San Marino", "Sao Tome and Principe", "Saudi Arabia", "Senegal", "Serbia", "Seychelles", "Sierra Leone", "Singapore", "Sint Maarten (Dutch part)", "Slovakia", "Slovenia", "Solomon Islands", "Somalia", "South Africa", "South Georgia and the South Sandwich Islands", "South Sudan", "Spain", "Sri Lanka", "Sudan", "Suriname", "Svalbard and Jan Mayen", "Swaziland", "Sweden", "Switzerland", "Syrian Arab Republic", "Taiwan, Province of China", "Tajikistan", "United Republic of Tanzania", "Thailand", "Timor-Leste", "Togo", "Tokelau", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan", "Turks and Caicos Islands", "Tuvalu", "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", "United States", "United States Minor Outlying Islands", "Uruguay", "Uzbekistan", "Vanuatu", "Venezuela", "Viet Nam", "Virgin Islands (British)", "US Virgin Islands", "Wallis and Futuna", "Western Sahara", "Yemen", "Zambia", "Zimbabwe"};
// note: from the original list https://www.worldatlas.com/aatlas/ctycodes.htm, I modified the code of "Dominican Republic", I removed the "-" sign from all codes and I corrected some typos in the country names
public static final String[] COUNTRY_CODES = {"93", "355", "213", "1684", "376", "244", "1264", "672", "1268", "54", "374", "297", "61", "43", "994", "1242", "973", "880", "1246", "375", "32", "501", "229", "1441", "975", "591", "599", "387", "267", "47", "55", "246", "673", "359", "226", "257", "855", "237", "1", "238", "1345", "236", "235", "56", "86", "61", "61", "57", "269", "242", "243", "682", "506", "385", "53", "599", "357", "420", "225", "45", "253", "1767", "18", "593", "20", "503", "240", "291", "372", "251", "500", "298", "679", "358", "33", "594", "689", "262", "241", "220", "995", "49", "233", "350", "30", "299", "1473", "590", "1671", "502", "44", "224", "245", "592", "509", "672", "379", "504", "852", "36", "354", "91", "62", "98", "964", "353", "44", "972", "39", "1876", "81", "44", "962", "7", "254", "686", "850", "82", "965", "996", "856", "371", "961", "266", "231", "218", "423", "370", "352", "853", "389", "261", "265", "60", "960", "223", "356", "692", "596", "222", "230", "262", "52", "691", "373", "377", "976", "382", "1664", "212", "258", "95", "264", "674", "977", "31", "687", "64", "505", "227", "234", "683", "672", "1670", "47", "968", "92", "680", "970", "507", "675", "595", "51", "63", "870", "48", "351", "1", "974", "40", "7", "250", "262", "590", "290", "1869", "1758", "590", "508", "1784", "685", "378", "239", "966", "221", "381", "248", "232", "65", "1721", "421", "386", "677", "252", "27", "500", "211", "34", "94", "249", "597", "47", "268", "46", "41", "963", "886", "992", "255", "66", "670", "228", "690", "676", "1868", "216", "90", "993", "1649", "688", "256", "380", "971", "44", "1", "1", "598", "998", "678", "58", "84", "1284", "1340", "681", "212", "967", "260", "263"};
// here I list the file names in the same order of the previous country names and country codes; some unused flags are not listed, because we have more flags than country codes...
public static final String[] COUNTRY_FLAGS = {"af.png", "al.png", "dz.png", "as.png", "ad.png", "ao.png", "ai.png", "aq.png", "ag.png", "ar.png", "am.png", "aw.png", "au.png", "at.png", "az.png", "bs.png", "bh.png", "bd.png", "bb.png", "by.png", "be.png", "bz.png", "bj.png", "bm.png", "bt.png", "bo.png", "bq.png", "ba.png", "bw.png", "bv.png", "br.png", "io.png", "bn.png", "bg.png", "bf.png", "bi.png", "kh.png", "cm.png", "ca.png", "cv.png", "ky.png", "cf.png", "td.png", "cl.png", "cn.png", "cx.png", "cc.png", "co.png", "km.png", "cg.png", "cd.png", "ck.png", "cr.png", "hr.png", "cu.png", "cw.png", "cy.png", "cz.png", "ci.png", "dk.png", "dj.png", "dm.png", "do.png", "ec.png", "eg.png", "sv.png", "gq.png", "er.png", "ee.png", "et.png", "fk.png", "fo.png", "fj.png", "fi.png", "fr.png", "gf.png", "pf.png", "tf.png", "ga.png", "gm.png", "ge.png", "de.png", "gh.png", "gi.png", "gr.png", "gl.png", "gd.png", "gp.png", "gu.png", "gt.png", "gg.png", "gn.png", "gw.png", "gy.png", "ht.png", "hm.png", "va.png", "hn.png", "hk.png", "hu.png", "is.png", "in.png", "id.png", "ir.png", "iq.png", "ie.png", "im.png", "il.png", "it.png", "jm.png", "jp.png", "je.png", "jo.png", "kz.png", "ke.png", "ki.png", "kp.png", "kr.png", "kw.png", "kg.png", "la.png", "lv.png", "lb.png", "ls.png", "lr.png", "ly.png", "li.png", "lt.png", "lu.png", "mo.png", "mk.png", "mg.png", "mw.png", "my.png", "mv.png", "ml.png", "mt.png", "mh.png", "mq.png", "mr.png", "mu.png", "yt.png", "mx.png", "fm.png", "md.png", "mc.png", "mn.png", "me.png", "ms.png", "ma.png", "mz.png", "mm.png", "na.png", "nr.png", "np.png", "nl.png", "nc.png", "nz.png", "ni.png", "ne.png", "ng.png", "nu.png", "nf.png", "mp.png", "no.png", "om.png", "pk.png", "pw.png", "ps.png", "pa.png", "pg.png", "py.png", "pe.png", "ph.png", "pn.png", "pl.png", "pt.png", "pr.png", "qa.png", "ro.png", "ru.png", "rw.png", "re.png", "bl.png", "sh.png", "kn.png", "lc.png", "mf.png", "pm.png", "vc.png", "ws.png", "sm.png", "st.png", "sa.png", "sn.png", "rs.png", "sc.png", "sl.png", "sg.png", "sx.png", "sk.png", "si.png", "sb.png", "so.png", "za.png", "gs.png", "ss.png", "es.png", "lk.png", "sd.png", "sr.png", "sj.png", "sz.png", "se.png", "ch.png", "sy.png", "tw.png", "tj.png", "tz.png", "th.png", "tl.png", "tg.png", "tk.png", "to.png", "tt.png", "tn.png", "tr.png", "tm.png", "tc.png", "tv.png", "ug.png", "ua.png", "ae.png", "gb.png", "us.png", "um.png", "uy.png", "uz.png", "vu.png", "ve.png", "vn.png", "vg.png", "vi.png", "wf.png", "eh.png", "ye.png", "zm.png", "zw.png"};
// note that the file names should be lowercase, instead the country iso codes should be uppercase
public static final String[] COUNTRY_ISO2 = {"AF", "AL", "DZ", "AS", "AD", "AO", "AI", "AQ", "AG", "AR", "AM", "AW", "AU", "AT", "AZ", "BS", "BH", "BD", "BB", "BY", "BE", "BZ", "BJ", "BM", "BT", "BO", "BQ", "BA", "BW", "BV", "BR", "IO", "BN", "BG", "BF", "BI", "KH", "CM", "CA", "CV", "KY", "CF", "TD", "CL", "CN", "CX", "CC", "CO", "KM", "CG", "CD", "CK", "CR", "HR", "CU", "CW", "CY", "CZ", "CI", "DK", "DJ", "DM", "DO", "EC", "EG", "SV", "GQ", "ER", "EE", "ET", "FK", "FO", "FJ", "FI", "FR", "GF", "PF", "TF", "GA", "GM", "GE", "DE", "GH", "GI", "GR", "GL", "GD", "GP", "GU", "GT", "GG", "GN", "GW", "GY", "HT", "HM", "VA", "HN", "HK", "HU", "IS", "IN", "ID", "IR", "IQ", "IE", "IM", "IL", "IT", "JM", "JP", "JE", "JO", "KZ", "KE", "KI", "KP", "KR", "KW", "KG", "LA", "LV", "LB", "LS", "LR", "LY", "LI", "LT", "LU", "MO", "MK", "MG", "MW", "MY", "MV", "ML", "MT", "MH", "MQ", "MR", "MU", "YT", "MX", "FM", "MD", "MC", "MN", "ME", "MS", "MA", "MZ", "MM", "NA", "NR", "NP", "NL", "NC", "NZ", "NI", "NE", "NG", "NU", "NF", "MP", "NO", "OM", "PK", "PW", "PS", "PA", "PG", "PY", "PE", "PH", "PN", "PL", "PT", "PR", "QA", "RO", "RU", "RW", "RE", "BL", "SH", "KN", "LC", "MF", "PM", "VC", "WS", "SM", "ST", "SA", "SN", "RS", "SC", "SL", "SG", "SX", "SK", "SI", "SB", "SO", "ZA", "GS", "SS", "ES", "LK", "SD", "SR", "SJ", "SZ", "SE", "CH", "SY", "TW", "TJ", "TZ", "TH", "TL", "TG", "TK", "TO", "TT", "TN", "TR", "TM", "TC", "TV", "UG", "UA", "AE", "GB", "US", "UM", "UY", "UZ", "VU", "VE", "VN", "VG", "VI", "WF", "EH", "YE", "ZM", "ZW"};
public static final String[] COUNTRY_ISO3 = {"AFG", "ALB", "DZA", "ASM", "AND", "AGO", "AIA", "ATA", "ATG", "ARG", "ARM", "ABW", "AUS", "AUT", "AZE", "BHS", "BHR", "BGD", "BRB", "BLR", "BEL", "BLZ", "BEN", "BMU", "BTN", "BOL", "BES", "BIH", "BWA", "BVT", "BRA", "IOT", "BRN", "BGR", "BFA", "BDI", "KHM", "CMR", "CAN", "CPV", "CYM", "CAF", "TCD", "CHL", "CHN", "CXR", "CCK", "COL", "COM", "COG", "COD", "COK", "CRI", "HRV", "CUB", "CUW", "CYP", "CZE", "CIV", "DNK", "DJI", "DMA", "DOM", "ECU", "EGY", "SLV", "GNQ", "ERI", "EST", "ETH", "FLK", "FRO", "FJI", "FIN", "FRA", "GUF", "PYF", "ATF", "GAB", "GMB", "GEO", "DEU", "GHA", "GIB", "GRC", "GRL", "GRD", "GLP", "GUM", "GTM", "GGY", "GIN", "GNB", "GUY", "HTI", "HMD", "VAT", "HND", "HKG", "HUN", "ISL", "IND", "IDN", "IRN", "IRQ", "IRL", "IMN", "ISR", "ITA", "JAM", "JPN", "JEY", "JOR", "KAZ", "KEN", "KIR", "PRK", "KOR", "KWT", "KGZ", "LAO", "LVA", "LBN", "LSO", "LBR", "LBY", "LIE", "LTU", "LUX", "MAC", "MKD", "MDG", "MWI", "MYS", "MDV", "MLI", "MLT", "MHL", "MTQ", "MRT", "MUS", "MYT", "MEX", "FSM", "MDA", "MCO", "MNG", "MNE", "MSR", "MAR", "MOZ", "MMR", "NAM", "NRU", "NPL", "NLD", "NCL", "NZL", "NIC", "NER", "NGA", "NIU", "NFK", "MNP", "NOR", "OMN", "PAK", "PLW", "PSE", "PAN", "PNG", "PRY", "PER", "PHL", "PCN", "POL", "PRT", "PRI", "QAT", "ROU", "RUS", "RWA", "REU", "BLM", "SHN", "KNA", "LCA", "MAF", "SPM", "VCT", "WSM", "SMR", "STP", "SAU", "SEN", "SRB", "SYC", "SLE", "SGP", "SXM", "SVK", "SVN", "SLB", "SOM", "ZAF", "SGS", "SSD", "ESP", "LKA", "SDN", "SUR", "SJM", "SWZ", "SWE", "CHE", "SYR", "TWN", "TJK", "TZA", "THA", "TLS", "TGO", "TKL", "TON", "TTO", "TUN", "TUR", "TKM", "TCA", "TUV", "UGA", "UKR", "ARE", "GBR", "USA", "UMI", "URY", "UZB", "VUT", "VEN", "VNM", "VGB", "VIR", "WLF", "ESH", "YEM", "ZMB", "ZWE"};
STEP 4 - Put all together
From CountryCodePicker.java
and CountryPickerForm.java
(of the Uber Clone) remove the import of the arrays from com.codename1.sms.activation.ActivationForm
and insert the previous arrays in your package and import them from it.
In the Codename One Designer, create a new file 248flags.res
and import all the PNG flags generated at the Step 2 (http://jmp.sh/vXaA7eC) with "Images" -> "Quick Add Multiimages"
, selecting "4K"
as source resolution. Wait for a while, it can be a long task...
In the CountryCodePicker.java
, change:
flagResource = Resources.open("/flags.res");
to:
flagResource = Resources.open("/248flags.res");
STEP 5 - Result
Good news: now there are all the flags!!! :)
About the build size, the original flag.res
of SMSActivation.cn1lib
has a size of 411 kbyte
(it's located in lib/impl/cls/flags.res
), while my 248flags.res
has a size of 2596 kbyte
... but the original flag.res
uses normal images of about 100x50 pixels, instead I used multiimages that better adapt on different densities.
Final check: it's possibile to manually check all country names and codes on Wikipedia and on other resources... if somebody wants to do the check, thanks :)
Upvotes: 1
Reputation: 52760
I wish I knew. I looked a lot for a resource that contains all of the flags in the world and was very disappointed that most were lacking in various ways.
After I did all that I thought about using the unicode emojii flags which would be a huge boon as it would also remove the need for images but that wouldn't work in the simulator properly as it doesn't render emojii. It might be interesting to try and enhance the code with support for emojii on devices where it's supported but you will need to actually do the legwork of going through almost 200 countries and finding their flag unicode value... There are some lists but adapting them is a pain.
Upvotes: 1