Select Country Code window in Titanium

Posted By : Akshay Luthra | 26-Dec-2014

I am writing this blog to help you get a Country Code selection window in Titanium.

First, in your "app.js" file create a window. With that window create a Table View for listing the various countries, and a Done button.

 

The code for this is -


// this sets the background color of the master UIView

Titanium.UI.setBackgroundColor('#fff');



var win = Ti.UI.createWindow();



var contentView = Ti.UI.createView({

    height : Ti.UI.FILL,

    width : Ti.UI.FILL,

    top : 40,

    bottom : 60,

    layout : "vertical",

});

win.add(contentView);



var mainOptionsItems = [];

var optionSelected = null;

var country = null;

var data = [];



//////will export the country code json that I made in the next steps//////

mainOptionsItems = require("/countryCodes").countryCodes;



var optionsView = Ti.UI.createTableView({

    top : 0,

    width : Ti.Platform.displayCaps.platformWidth - 20,

    borderWidth : 2,

    borderColor : "#4fd1ff",

    showVerticalScrollIndicator : true,

    separatorColor : "transparent",

    separatorStyle : "NONE",

    minRowHeight : 40,

});



function populateCountryCodeList(mainOptionsItems) {

    for ( i = 0; i < mainOptionsItems.length; i++) {

        mainOptionsItems[i].index = i;

        /////you can see how rows of list is made in the next steps/////

        var row = require('/radioButtonRows').createRadioButtonRows(mainOptionsItems[i]);

        data.push(row);

    };

    optionsView.setData(data);

}



contentView.add(optionsView);



populateCountryCodeList(mainOptionsItems);



optionsView.addEventListener('click', function(e) {

    for ( j = 0; j < this.data[0].rows.length; j++) {

        if (e.row.id === this.data[0].rows[j].id) {

            e.row.check.setImage("/radioButtonChecked.png");

            optionSelected = e.row.optionValue;

            country = e.row.optionText;

        } else {

            this.data[0].rows[j].check.setImage("/radioButtonUnchecked.png");

        }

    };

});



///////done button//////

var doneButton = Ti.UI.createButton({

    backgroundColor : "#4fd1ff",

    color : "#fff",

    width : 120,

    height : 40,

    title : "Done",

    borderRadius : 4,

    bottom : 10,

});

win.add(doneButton);



doneButton.addEventListener('click', function(e) {

    if (optionSelected != null) {

        alert("Country : " + country + "\nCountry Code : " + optionSelected);

    } else {

        alert("Please select a country");

    }

});



win.open();

 

You can see that a Country Codes file is require in above code. Create a "countryCodes.js" file in your Resources directory and write the following code in it -



var countryCodes = [{

    text : "Afghanistan",

    id : "+93",

    code : "AF"

}, {

    text : "Albania",

    id : "+355",

    code : "AL"

}, {

    text : "Algeria",

    id : "+213",

    code : "DZ"

}, {

    text : "AmericanSamoa",

    id : "+1 684",

    code : "AS"

}, {

    text : "Andorra",

    id : "+376",

    code : "AD"

}, {

    text : "Angola",

    id : "+244",

    code : "AO"

}, {

    text : "Anguilla",

    id : "+1 264",

    code : "AI"

}, {

    text : "Antarctica",

    id : "+672",

    code : "AQ"

}, {

    text : "Antigua and Barbuda",

    id : "+1268",

    code : "AG"

}, {

    text : "Argentina",

    id : "+54",

    code : "AR"

}, {

    text : "Armenia",

    id : "+374",

    code : "AM"

}, {

    text : "Aruba",

    id : "+297",

    code : "AW"

}, {

    text : "Australia",

    id : "+61",

    code : "AU"

}, {

    text : "Austria",

    id : "+43",

    code : "AT"

}, {

    text : "Azerbaijan",

    id : "+994",

    code : "AZ"

}, {

    text : "Bahamas",

    id : "+1 242",

    code : "BS"

}, {

    text : "Bahrain",

    id : "+973",

    code : "BH"

}, {

    text : "Bangladesh",

    id : "+880",

    code : "BD"

}, {

    text : "Barbados",

    id : "+1 246",

    code : "BB"

}, {

    text : "Belarus",

    id : "+375",

    code : "BY"

}, {

    text : "Belgium",

    id : "+32",

    code : "BE"

}, {

    text : "Belize",

    id : "+501",

    code : "BZ"

}, {

    text : "Benin",

    id : "+229",

    code : "BJ"

}, {

    text : "Bermuda",

    id : "+1 441",

    code : "BM"

}, {

    text : "Bhutan",

    id : "+975",

    code : "BT"

}, {

    text : "Bolivia, Plurinational State of Bolivia",

    id : "+591",

    code : "BO"

}, {

    text : "Bosnia and Herzegovina",

    id : "+387",

    code : "BA"

}, {

    text : "Botswana",

    id : "+267",

    code : "BW"

}, {

    text : "Brazil",

    id : "+55",

    code : "BR"

}, {

    text : "British Indian Ocean Territory",

    id : "+246",

    code : "IO"

}, {

    text : "Brunei Darussalam",

    id : "+673",

    code : "BN"

}, {

    text : "Bulgaria",

    id : "+359",

    code : "BG"

}, {

    text : "Burkina Faso",

    id : "+226",

    code : "BF"

}, {

    text : "Burundi",

    id : "+257",

    code : "BI"

}, {

    text : "Cambodia",

    id : "+855",

    code : "KH"

}, {

    text : "Cameroon",

    id : "+237",

    code : "CM"

}, {

    text : "Canada",

    id : "+1",

    code : "CA"

}, {

    text : "Cape Verde",

    id : "+238",

    code : "CV"

}, {

    text : "Cayman Islands",

    id : "+ 345",

    code : "KY"

}, {

    text : "Central African Republic",

    id : "+236",

    code : "CF"

}, {

    text : "Chad",

    id : "+235",

    code : "TD"

}, {

    text : "Chile",

    id : "+56",

    code : "CL"

}, {

    text : "China",

    id : "+86",

    code : "CN"

}, {

    text : "Christmas Island",

    id : "+61",

    code : "CX"

}, {

    text : "Cocos (Keeling) Islands",

    id : "+61",

    code : "CC"

}, {

    text : "Colombia",

    id : "+57",

    code : "CO"

}, {

    text : "Comoros",

    id : "+269",

    code : "KM"

}, {

    text : "Congo",

    id : "+242",

    code : "CG"

}, {

    text : "Congo, The Democratic Republic of the Congo",

    id : "+243",

    code : "CD"

}, {

    text : "Cook Islands",

    id : "+682",

    code : "CK"

}, {

    text : "Costa Rica",

    id : "+506",

    code : "CR"

}, {

    text : "Cote d'Ivoire",

    id : "+225",

    code : "CI"

}, {

    text : "Croatia",

    id : "+385",

    code : "HR"

}, {

    text : "Cuba",

    id : "+53",

    code : "CU"

}, {

    text : "Cyprus",

    id : "+357",

    code : "CY"

}, {

    text : "Czech Republic",

    id : "+420",

    code : "CZ"

}, {

    text : "Denmark",

    id : "+45",

    code : "DK"

}, {

    text : "Djibouti",

    id : "+253",

    code : "DJ"

}, {

    text : "Dominica",

    id : "+1 767",

    code : "DM"

}, {

    text : "Dominican Republic",

    id : "+1 849",

    code : "DO"

}, {

    text : "Ecuador",

    id : "+593",

    code : "EC"

}, {

    text : "Egypt",

    id : "+20",

    code : "EG"

}, {

    text : "El Salvador",

    id : "+503",

    code : "SV"

}, {

    text : "Equatorial Guinea",

    id : "+240",

    code : "GQ"

}, {

    text : "Eritrea",

    id : "+291",

    code : "ER"

}, {

    text : "Estonia",

    id : "+372",

    code : "EE"

}, {

    text : "Ethiopia",

    id : "+251",

    code : "ET"

}, {

    text : "Falkland Islands (Malvinas)",

    id : "+500",

    code : "FK"

}, {

    text : "Faroe Islands",

    id : "+298",

    code : "FO"

}, {

    text : "Fiji",

    id : "+679",

    code : "FJ"

}, {

    text : "Finland",

    id : "+358",

    code : "FI"

}, {

    text : "France",

    id : "+33",

    code : "FR"

}, {

    text : "French Guiana",

    id : "+594",

    code : "GF"

}, {

    text : "French Polynesia",

    id : "+689",

    code : "PF"

}, {

    text : "Gabon",

    id : "+241",

    code : "GA"

}, {

    text : "Gambia",

    id : "+220",

    code : "GM"

}, {

    text : "Georgia",

    id : "+995",

    code : "GE"

}, {

    text : "Germany",

    id : "+49",

    code : "DE"

}, {

    text : "Ghana",

    id : "+233",

    code : "GH"

}, {

    text : "Gibraltar",

    id : "+350",

    code : "GI"

}, {

    text : "Greece",

    id : "+30",

    code : "GR"

}, {

    text : "Greenland",

    id : "+299",

    code : "GL"

}, {

    text : "Grenada",

    id : "+1 473",

    code : "GD"

}, {

    text : "Guadeloupe",

    id : "+590",

    code : "GP"

}, {

    text : "Guam",

    id : "+1 671",

    code : "GU"

}, {

    text : "Guatemala",

    id : "+502",

    code : "GT"

}, {

    text : "Guernsey",

    id : "+44",

    code : "GG"

}, {

    text : "Guinea",

    id : "+224",

    code : "GN"

}, {

    text : "Guinea-Bissau",

    id : "+245",

    code : "GW"

}, {

    text : "Guyana",

    id : "+595",

    code : "GY"

}, {

    text : "Haiti",

    id : "+509",

    code : "HT"

}, {

    text : "Holy See (Vatican City State)",

    id : "+379",

    code : "VA"

}, {

    text : "Honduras",

    id : "+504",

    code : "HN"

}, {

    text : "Hong Kong",

    id : "+852",

    code : "HK"

}, {

    text : "Hungary",

    id : "+36",

    code : "HU"

}, {

    text : "Iceland",

    id : "+354",

    code : "IS"

}, {

    text : "India",

    id : "+91",

    code : "IN"

}, {

    text : "Indonesia",

    id : "+62",

    code : "ID"

}, {

    text : "Iran, Islamic Republic of Iran",

    id : "+98",

    code : "IR"

}, {

    text : "Iraq",

    id : "+964",

    code : "IQ"

}, {

    text : "Ireland",

    id : "+353",

    code : "IE"

}, {

    text : "Isle of Man",

    id : "+44",

    code : "IM"

}, {

    text : "Israel",

    id : "+972",

    code : "IL"

}, {

    text : "Italy",

    id : "+39",

    code : "IT"

}, {

    text : "Jamaica",

    id : "+1 876",

    code : "JM"

}, {

    text : "Japan",

    id : "+81",

    code : "JP"

}, {

    text : "Jersey",

    id : "+44",

    code : "JE"

}, {

    text : "Jordan",

    id : "+962",

    code : "JO"

}, {

    text : "Kazakhstan",

    id : "+7 7",

    code : "KZ"

}, {

    text : "Kenya",

    id : "+254",

    code : "KE"

}, {

    text : "Kiribati",

    id : "+686",

    code : "KI"

}, {

    text : "Korea, Democratic People's Republic of Korea",

    id : "+850",

    code : "KP"

}, {

    text : "Korea, Republic of Korea",

    id : "+82",

    code : "KR"

}, {

    text : "Kuwait",

    id : "+965",

    code : "KW"

}, {

    text : "Kyrgyzstan",

    id : "+996",

    code : "KG"

}, {

    text : "Lao People's Democratic Republic",

    id : "+856",

    code : "LA"

}, {

    text : "Latvia",

    id : "+371",

    code : "LV"

}, {

    text : "Lebanon",

    id : "+961",

    code : "LB"

}, {

    text : "Lesotho",

    id : "+266",

    code : "LS"

}, {

    text : "Liberia",

    id : "+231",

    code : "LR"

}, {

    text : "Libyan Arab Jamahiriya",

    id : "+218",

    code : "LY"

}, {

    text : "Liechtenstein",

    id : "+423",

    code : "LI"

}, {

    text : "Lithuania",

    id : "+370",

    code : "LT"

}, {

    text : "Luxembourg",

    id : "+352",

    code : "LU"

}, {

    text : "Macao",

    id : "+853",

    code : "MO"

}, {

    text : "Macedonia, The Former Yugoslav Republic of Macedonia",

    id : "+389",

    code : "MK"

}, {

    text : "Madagascar",

    id : "+261",

    code : "MG"

}, {

    text : "Malawi",

    id : "+265",

    code : "MW"

}, {

    text : "Malaysia",

    id : "+60",

    code : "MY"

}, {

    text : "Maldives",

    id : "+960",

    code : "MV"

}, {

    text : "Mali",

    id : "+223",

    code : "ML"

}, {

    text : "Malta",

    id : "+356",

    code : "MT"

}, {

    text : "Marshall Islands",

    id : "+692",

    code : "MH"

}, {

    text : "Martinique",

    id : "+596",

    code : "MQ"

}, {

    text : "Mauritania",

    id : "+222",

    code : "MR"

}, {

    text : "Mauritius",

    id : "+230",

    code : "MU"

}, {

    text : "Mayotte",

    id : "+262",

    code : "YT"

}, {

    text : "Mexico",

    id : "+52",

    code : "MX"

}, {

    text : "Micronesia, Federated States of Micronesia",

    id : "+691",

    code : "FM"

}, {

    text : "Moldova, Republic of Moldova",

    id : "+373",

    code : "MD"

}, {

    text : "Monaco",

    id : "+377",

    code : "MC"

}, {

    text : "Mongolia",

    id : "+976",

    code : "MN"

}, {

    text : "Montenegro",

    id : "+382",

    code : "ME"

}, {

    text : "Montserrat",

    id : "+1664",

    code : "MS"

}, {

    text : "Morocco",

    id : "+212",

    code : "MA"

}, {

    text : "Mozambique",

    id : "+258",

    code : "MZ"

}, {

    text : "Myanmar",

    id : "+95",

    code : "MM"

}, {

    text : "Namibia",

    id : "+264",

    code : "NA"

}, {

    text : "Nauru",

    id : "+674",

    code : "NR"

}, {

    text : "Nepal",

    id : "+977",

    code : "NP"

}, {

    text : "Netherlands",

    id : "+31",

    code : "NL"

}, {

    text : "Netherlands Antilles",

    id : "+599",

    code : "AN"

}, {

    text : "New Caledonia",

    id : "+687",

    code : "NC"

}, {

    text : "New Zealand",

    id : "+64",

    code : "NZ"

}, {

    text : "Nicaragua",

    id : "+505",

    code : "NI"

}, {

    text : "Niger",

    id : "+227",

    code : "NE"

}, {

    text : "Nigeria",

    id : "+234",

    code : "NG"

}, {

    text : "Niue",

    id : "+683",

    code : "NU"

}, {

    text : "Norfolk Island",

    id : "+672",

    code : "NF"

}, {

    text : "Northern Mariana Islands",

    id : "+1 670",

    code : "MP"

}, {

    text : "Norway",

    id : "+47",

    code : "NO"

}, {

    text : "Oman",

    id : "+968",

    code : "OM"

}, {

    text : "Pakistan",

    id : "+92",

    code : "PK"

}, {

    text : "Palau",

    id : "+680",

    code : "PW"

}, {

    text : "Palestinian Territory, Occupied",

    id : "+970",

    code : "PS"

}, {

    text : "Panama",

    id : "+507",

    code : "PA"

}, {

    text : "Papua New Guinea",

    id : "+675",

    code : "PG"

}, {

    text : "Paraguay",

    id : "+595",

    code : "PY"

}, {

    text : "Peru",

    id : "+51",

    code : "PE"

}, {

    text : "Philippines",

    id : "+63",

    code : "PH"

}, {

    text : "Pitcairn",

    id : "+872",

    code : "PN"

}, {

    text : "Poland",

    id : "+48",

    code : "PL"

}, {

    text : "Portugal",

    id : "+351",

    code : "PT"

}, {

    text : "Puerto Rico",

    id : "+1 939",

    code : "PR"

}, {

    text : "Qatar",

    id : "+974",

    code : "QA"

}, {

    text : "Romania",

    id : "+40",

    code : "RO"

}, {

    text : "Russia",

    id : "+7",

    code : "RU"

}, {

    text : "Rwanda",

    id : "+250",

    code : "RW"

}, {

    text : "Réunion",

    id : "+262",

    code : "RE"

}, {

    text : "Saint Barthélemy",

    id : "+590",

    code : "BL"

}, {

    text : "Saint Helena, Ascension and Tristan Da Cunha",

    id : "+290",

    code : "SH"

}, {

    text : "Saint Kitts and Nevis",

    id : "+1 869",

    code : "KN"

}, {

    text : "Saint Lucia",

    id : "+1 758",

    code : "LC"

}, {

    text : "Saint Martin",

    id : "+590",

    code : "MF"

}, {

    text : "Saint Pierre and Miquelon",

    id : "+508",

    code : "PM"

}, {

    text : "Saint Vincent and the Grenadines",

    id : "+1 784",

    code : "VC"

}, {

    text : "Samoa",

    id : "+685",

    code : "WS"

}, {

    text : "San Marino",

    id : "+378",

    code : "SM"

}, {

    text : "Sao Tome and Principe",

    id : "+239",

    code : "ST"

}, {

    text : "Saudi Arabia",

    id : "+966",

    code : "SA"

}, {

    text : "Senegal",

    id : "+221",

    code : "SN"

}, {

    text : "Serbia",

    id : "+381",

    code : "RS"

}, {

    text : "Seychelles",

    id : "+248",

    code : "SC"

}, {

    text : "Sierra Leone",

    id : "+232",

    code : "SL"

}, {

    text : "Singapore",

    id : "+65",

    code : "SG"

}, {

    text : "Slovakia",

    id : "+421",

    code : "SK"

}, {

    text : "Slovenia",

    id : "+386",

    code : "SI"

}, {

    text : "Solomon Islands",

    id : "+677",

    code : "SB"

}, {

    text : "Somalia",

    id : "+252",

    code : "SO"

}, {

    text : "South Africa",

    id : "+27",

    code : "ZA"

}, {

    text : "South Georgia and the South Sandwich Islands",

    id : "+500",

    code : "GS"

}, {

    text : "Spain",

    id : "+34",

    code : "ES"

}, {

    text : "Sri Lanka",

    id : "+94",

    code : "LK"

}, {

    text : "Sudan",

    id : "+249",

    code : "SD"

}, {

    text : "Suriname",

    id : "+597",

    code : "SR"

}, {

    text : "Svalbard and Jan Mayen",

    id : "+47",

    code : "SJ"

}, {

    text : "Swaziland",

    id : "+268",

    code : "SZ"

}, {

    text : "Sweden",

    id : "+46",

    code : "SE"

}, {

    text : "Switzerland",

    id : "+41",

    code : "CH"

}, {

    text : "Syrian Arab Republic",

    id : "+963",

    code : "SY"

}, {

    text : "Taiwan, Province of China",

    id : "+886",

    code : "TW"

}, {

    text : "Tajikistan",

    id : "+992",

    code : "TJ"

}, {

    text : "Tanzania, United Republic of Tanzania",

    id : "+255",

    code : "TZ"

}, {

    text : "Thailand",

    id : "+66",

    code : "TH"

}, {

    text : "Timor-Leste",

    id : "+670",

    code : "TL"

}, {

    text : "Togo",

    id : "+228",

    code : "TG"

}, {

    text : "Tokelau",

    id : "+690",

    code : "TK"

}, {

    text : "Tonga",

    id : "+676",

    code : "TO"

}, {

    text : "Trinidad and Tobago",

    id : "+1 868",

    code : "TT"

}, {

    text : "Tunisia",

    id : "+216",

    code : "TN"

}, {

    text : "Turkey",

    id : "+90",

    code : "TR"

}, {

    text : "Turkmenistan",

    id : "+993",

    code : "TM"

}, {

    text : "Turks and Caicos Islands",

    id : "+1 649",

    code : "TC"

}, {

    text : "Tuvalu",

    id : "+688",

    code : "TV"

}, {

    text : "Uganda",

    id : "+256",

    code : "UG"

}, {

    text : "Ukraine",

    id : "+380",

    code : "UA"

}, {

    text : "United Arab Emirates",

    id : "+971",

    code : "AE"

}, {

    text : "United Kingdom",

    id : "+44",

    code : "GB"

}, {

    text : "United States",

    id : "+1",

    code : "US"

}, {

    text : "Uruguay",

    id : "+598",

    code : "UY"

}, {

    text : "Uzbekistan",

    id : "+998",

    code : "UZ"

}, {

    text : "Vanuatu",

    id : "+678",

    code : "VU"

}, {

    text : "Venezuela, Bolivarian Republic of Venezuela",

    id : "+58",

    code : "VE"

}, {

    text : "Viet Nam",

    id : "+84",

    code : "VN"

}, {

    text : "Virgin Islands, British",

    id : "+1 284",

    code : "VG"

}, {

    text : "Virgin Islands, U.S.",

    id : "+1 340",

    code : "VI"

}, {

    text : "Wallis and Futuna",

    id : "+681",

    code : "WF"

}, {

    text : "Yemen",

    id : "+967",

    code : "YE"

}, {

    text : "Zambia",

    id : "+260",

    code : "ZM"

}, {

    text : "Zimbabwe",

    id : "+263",

    code : "ZW"

}, {

    text : "Åland Islands",

    id : "+358",

    code : "AX"

}];

exports.countryCodes = countryCodes;

 

Now for creating the rows of Table View that has been used, create a "radioButtonRows.js" file in your Resources directory and write the following code in it -



exports.createRadioButtonRows = function(params) {

    params = params || {};



    var optionId = params.id;

    var optionText = params.text;

    var row = Ti.UI.createTableViewRow({

        checked : false,

        height : '50dp',

        layout : 'horizontal',

        optionValue : optionId,

        optionText : optionText,

        id : params.index,

    });

    if (Ti.Platform.osname === 'android') {

        row.backgroundSelectedColor = "transparent";

    } else {

        row.selectedBackgroundColor = "transparent";

    }



    var radioButton = Ti.UI.createImageView({

        image : "/radioButtonUnchecked.png",

        left : '10dp',

        right : '10dp',

        height : "20dp",

        width : "20dp",

    });

    row.check = radioButton;

    row.add(radioButton);



    var optionLabel = Ti.UI.createLabel({

        text : optionText,

        color : '#444747',

        font : {

            fontFamily : 'ITC Avant Grade Gothic Demi',

            fontSize : "14sp",

        },

        left : '10dp',

        height : 40,

    });

    row.add(optionLabel);



return row;



}; 

 

Unselected

Selected

Country Code List

About Author

Author Image
Akshay Luthra

Akshay Luthra has excellent experience in developing Cross Platform Mobile Apps using JavaScript and Titanium Framework. He loves listening to music and travelling new places.

Request for Proposal

Name is required

Comment is required

Sending message..