Country Picker in Android (Java/Kotlin) code

Country Picker in Android (Java/Kotlin)


Please Subscribe Youtube| Like Facebook | Follow Twitter

Country Picker in Android (Java/Kotlin)

In this tutorial, we will learn how to create Country Picker in Android (Java/Kotlin) application. The country picker dropdown with flags will allow users to search for countries, and choose a country, then selected country’s country name, country code and country phone code will be toasted.

country picker android
search country picker android
country picker android java kotlin
country picker android with flag code and phone code

Implementation In Java

Step 1 : Add the necessary dependencies in the build.gradle file

First, make sure to add the necessary dependencies in the build.gradle file of your app module to include EmojiCompat support as well as gson library.

implementation 'androidx.emoji:emoji-bundled:1.1.0'
implementation 'com.google.code.gson:gson:2.8.9'

Step 2 : Add a JSON file (countries.json) to the assets folder to store the list of countries and their information.

  • In Android Studio, locate the “Project” pane on the left side of the window.
  • Navigate to the “app/src/main” directory in the “Project” pane.
  • Right-click on the “main” directory and select “New” -> “Directory” -> “assets” This will create an “assets” folder inside the “main” directory.
  • Inside the “assets” directory, right-click and select “New” -> “File”. Name the file countries.json.
  • Open the countries.json file and add the JSON data for your list of countries.

countries.json file

[
  {
    "name": "Afghanistan",
    "code": "AF",
    "phoneCode": "+93",
    "emoji": "๐Ÿ‡ฆ๐Ÿ‡ซ"
  },
  {
    "name": "Albania",
    "code": "AL",
    "phoneCode": "+355",
    "emoji": "๐Ÿ‡ฆ๐Ÿ‡ฑ"
  },
  {
    "name": "Algeria",
    "code": "DZ",
    "phoneCode": "+213",
    "emoji": "๐Ÿ‡ฉ๐Ÿ‡ฟ"
  },
  {
    "name": "Andorra",
    "code": "AD",
    "phoneCode": "+376",
    "emoji": "๐Ÿ‡ฆ๐Ÿ‡ฉ"
  },
  {
    "name": "Angola",
    "code": "AO",
    "phoneCode": "+244",
    "emoji": "๐Ÿ‡ฆ๐Ÿ‡ด"
  },
  {
    "name": "Antigua and Barbuda",
    "code": "AG",
    "phoneCode": "+1",
    "emoji": "๐Ÿ‡ฆ๐Ÿ‡ฌ"
  },
  {
    "name": "Argentina",
    "code": "AR",
    "phoneCode": "+54",
    "emoji": "๐Ÿ‡ฆ๐Ÿ‡ท"
  },
  {
    "name": "Armenia",
    "code": "AM",
    "phoneCode": "+374",
    "emoji": "๐Ÿ‡ฆ๐Ÿ‡ฒ"
  },
  {
    "name": "Australia",
    "code": "AU",
    "phoneCode": "+61",
    "emoji": "๐Ÿ‡ฆ๐Ÿ‡บ"
  },
  {
    "name": "Austria",
    "code": "AT",
    "phoneCode": "+43",
    "emoji": "๐Ÿ‡ฆ๐Ÿ‡น"
  },
  {
    "name": "Azerbaijan",
    "code": "AZ",
    "phoneCode": "+994",
    "emoji": "๐Ÿ‡ฆ๐Ÿ‡ฟ"
  },
  {
    "name": "Bahamas",
    "code": "BS",
    "phoneCode": "+1",
    "emoji": "๐Ÿ‡ง๐Ÿ‡ธ"
  },
  {
    "name": "Bahrain",
    "code": "BH",
    "phoneCode": "+973",
    "emoji": "๐Ÿ‡ง๐Ÿ‡ญ"
  },
  {
    "name": "Bangladesh",
    "code": "BD",
    "phoneCode": "+880",
    "emoji": "๐Ÿ‡ง๐Ÿ‡ฉ"
  },
  {
    "name": "Barbados",
    "code": "BB",
    "phoneCode": "+1",
    "emoji": "๐Ÿ‡ง๐Ÿ‡ง"
  },
  {
    "name": "Belarus",
    "code": "BY",
    "phoneCode": "+375",
    "emoji": "๐Ÿ‡ง๐Ÿ‡พ"
  },
  {
    "name": "Belgium",
    "code": "BE",
    "phoneCode": "+32",
    "emoji": "๐Ÿ‡ง๐Ÿ‡ช"
  },
  {
    "name": "Belize",
    "code": "BZ",
    "phoneCode": "+501",
    "emoji": "๐Ÿ‡ง๐Ÿ‡ฟ"
  },
  {
    "name": "Benin",
    "code": "BJ",
    "phoneCode": "+229",
    "emoji": "๐Ÿ‡ง๐Ÿ‡ฏ"
  },
  {
    "name": "Bhutan",
    "code": "BT",
    "phoneCode": "+975",
    "emoji": "๐Ÿ‡ง๐Ÿ‡น"
  },
  {
    "name": "Bolivia",
    "code": "BO",
    "phoneCode": "+591",
    "emoji": "๐Ÿ‡ง๐Ÿ‡ด"
  },
  {
    "name": "Bosnia and Herzegovina",
    "code": "BA",
    "phoneCode": "+387",
    "emoji": "๐Ÿ‡ง๐Ÿ‡ฆ"
  },
  {
    "name": "Botswana",
    "code": "BW",
    "phoneCode": "+267",
    "emoji": "๐Ÿ‡ง๐Ÿ‡ผ"
  },
  {
    "name": "Brazil",
    "code": "BR",
    "phoneCode": "+55",
    "emoji": "๐Ÿ‡ง๐Ÿ‡ท"
  },
  {
    "name": "Brunei",
    "code": "BN",
    "phoneCode": "+673",
    "emoji": "๐Ÿ‡ง๐Ÿ‡ณ"
  },
  {
    "name": "Bulgaria",
    "code": "BG",
    "phoneCode": "+359",
    "emoji": "๐Ÿ‡ง๐Ÿ‡ฌ"
  },
  {
    "name": "Burkina Faso",
    "code": "BF",
    "phoneCode": "+226",
    "emoji": "๐Ÿ‡ง๐Ÿ‡ซ"
  },
  {
    "name": "Burundi",
    "code": "BI",
    "phoneCode": "+257",
    "emoji": "๐Ÿ‡ง๐Ÿ‡ฎ"
  },
  {
    "name": "Cabo Verde",
    "code": "CV",
    "phoneCode": "+238",
    "emoji": "๐Ÿ‡จ๐Ÿ‡ป"
  },
  {
    "name": "Cambodia",
    "code": "KH",
    "phoneCode": "+855",
    "emoji": "๐Ÿ‡ฐ๐Ÿ‡ญ"
  },
  {
    "name": "Cameroon",
    "code": "CM",
    "phoneCode": "+237",
    "emoji": "๐Ÿ‡จ๐Ÿ‡ฒ"
  },
  {
    "name": "Canada",
    "code": "CA",
    "phoneCode": "+1",
    "emoji": "๐Ÿ‡จ๐Ÿ‡ฆ"
  },
  {
    "name": "Central African Republic",
    "code": "CF",
    "phoneCode": "+236",
    "emoji": "๐Ÿ‡จ๐Ÿ‡ซ"
  },
  {
    "name": "Chad",
    "code": "TD",
    "phoneCode": "+235",
    "emoji": "๐Ÿ‡น๐Ÿ‡ฉ"
  },
  {
    "name": "Chile",
    "code": "CL",
    "phoneCode": "+56",
    "emoji": "๐Ÿ‡จ๐Ÿ‡ฑ"
  },
  {
    "name": "China",
    "code": "CN",
    "phoneCode": "+86",
    "emoji": "๐Ÿ‡จ๐Ÿ‡ณ"
  },
  {
    "name": "Colombia",
    "code": "CO",
    "phoneCode": "+57",
    "emoji": "๐Ÿ‡จ๐Ÿ‡ด"
  },
  {
    "name": "Comoros",
    "code": "KM",
    "phoneCode": "+269",
    "emoji": "๐Ÿ‡ฐ๐Ÿ‡ฒ"
  },
  {
    "name": "Congo",
    "code": "CG",
    "phoneCode": "+242",
    "emoji": "๐Ÿ‡จ๐Ÿ‡ฌ"
  },
  {
    "name": "Costa Rica",
    "code": "CR",
    "phoneCode": "+506",
    "emoji": "๐Ÿ‡จ๐Ÿ‡ท"
  },
  {
    "name": "Cรดte d'Ivoire",
    "code": "CI",
    "phoneCode": "+225",
    "emoji": "๐Ÿ‡จ๐Ÿ‡ฎ"
  },
  {
    "name": "Croatia",
    "code": "HR",
    "phoneCode": "+385",
    "emoji": "๐Ÿ‡ญ๐Ÿ‡ท"
  },
  {
    "name": "Cuba",
    "code": "CU",
    "phoneCode": "+53",
    "emoji": "๐Ÿ‡จ๐Ÿ‡บ"
  },
  {
    "name": "Cyprus",
    "code": "CY",
    "phoneCode": "+357",
    "emoji": "๐Ÿ‡จ๐Ÿ‡พ"
  },
  {
    "name": "Czech Republic",
    "code": "CZ",
    "phoneCode": "+420",
    "emoji": "๐Ÿ‡จ๐Ÿ‡ฟ"
  },
  {
    "name": "Democratic Republic of the Congo",
    "code": "CD",
    "phoneCode": "+243",
    "emoji": "๐Ÿ‡จ๐Ÿ‡ฉ"
  },
  {
    "name": "Denmark",
    "code": "DK",
    "phoneCode": "+45",
    "emoji": "๐Ÿ‡ฉ๐Ÿ‡ฐ"
  },
  {
    "name": "Djibouti",
    "code": "DJ",
    "phoneCode": "+253",
    "emoji": "๐Ÿ‡ฉ๐Ÿ‡ฏ"
  },
  {
    "name": "Dominica",
    "code": "DM",
    "phoneCode": "+1",
    "emoji": "๐Ÿ‡ฉ๐Ÿ‡ฒ"
  },
  {
    "name": "Dominican Republic",
    "code": "DO",
    "phoneCode": "+1",
    "emoji": "๐Ÿ‡ฉ๐Ÿ‡ด"
  },
  {
    "name": "Ecuador",
    "code": "EC",
    "phoneCode": "+593",
    "emoji": "๐Ÿ‡ช๐Ÿ‡จ"
  },
  {
    "name": "Egypt",
    "code": "EG",
    "phoneCode": "+20",
    "emoji": "๐Ÿ‡ช๐Ÿ‡ฌ"
  },
  {
    "name": "El Salvador",
    "code": "SV",
    "phoneCode": "+503",
    "emoji": "๐Ÿ‡ธ๐Ÿ‡ป"
  },
  {
    "name": "Equatorial Guinea",
    "code": "GQ",
    "phoneCode": "+240",
    "emoji": "๐Ÿ‡ฌ๐Ÿ‡ถ"
  },
  {
    "name": "Eritrea",
    "code": "ER",
    "phoneCode": "+291",
    "emoji": "๐Ÿ‡ช๐Ÿ‡ท"
  },
  {
    "name": "Estonia",
    "code": "EE",
    "phoneCode": "+372",
    "emoji": "๐Ÿ‡ช๐Ÿ‡ช"
  },
  {
    "name": "Ethiopia",
    "code": "ET",
    "phoneCode": "+251",
    "emoji": "๐Ÿ‡ช๐Ÿ‡น"
  },
  {
    "name": "Fiji",
    "code": "FJ",
    "phoneCode": "+679",
    "emoji": "๐Ÿ‡ซ๐Ÿ‡ฏ"
  },
  {
    "name": "Finland",
    "code": "FI",
    "phoneCode": "+358",
    "emoji": "๐Ÿ‡ซ๐Ÿ‡ฎ"
  },
  {
    "name": "France",
    "code": "FR",
    "phoneCode": "+33",
    "emoji": "๐Ÿ‡ซ๐Ÿ‡ท"
  },
  {
    "name": "Gabon",
    "code": "GA",
    "phoneCode": "+241",
    "emoji": "๐Ÿ‡ฌ๐Ÿ‡ฆ"
  },
  {
    "name": "Gambia",
    "code": "GM",
    "phoneCode": "+220",
    "emoji": "๐Ÿ‡ฌ๐Ÿ‡ฒ"
  },
  {
    "name": "Georgia",
    "code": "GE",
    "phoneCode": "+995",
    "emoji": "๐Ÿ‡ฌ๐Ÿ‡ช"
  },
  {
    "name": "Germany",
    "code": "DE",
    "phoneCode": "+49",
    "emoji": "๐Ÿ‡ฉ๐Ÿ‡ช"
  },
  {
    "name": "Ghana",
    "code": "GH",
    "phoneCode": "+233",
    "emoji": "๐Ÿ‡ฌ๐Ÿ‡ญ"
  },
  {
    "name": "Greece",
    "code": "GR",
    "phoneCode": "+30",
    "emoji": "๐Ÿ‡ฌ๐Ÿ‡ท"
  },
  {
    "name": "Grenada",
    "code": "GD",
    "phoneCode": "+1",
    "emoji": "๐Ÿ‡ฌ๐Ÿ‡ฉ"
  },
  {
    "name": "Guatemala",
    "code": "GT",
    "phoneCode": "+502",
    "emoji": "๐Ÿ‡ฌ๐Ÿ‡น"
  },
  {
    "name": "Guinea",
    "code": "GN",
    "phoneCode": "+224",
    "emoji": "๐Ÿ‡ฌ๐Ÿ‡ณ"
  },
  {
    "name": "Guinea-Bissau",
    "code": "GW",
    "phoneCode": "+245",
    "emoji": "๐Ÿ‡ฌ๐Ÿ‡ผ"
  },
  {
    "name": "Guyana",
    "code": "GY",
    "phoneCode": "+592",
    "emoji": "๐Ÿ‡ฌ๐Ÿ‡พ"
  },
  {
    "name": "Haiti",
    "code": "HT",
    "phoneCode": "+509",
    "emoji": "๐Ÿ‡ญ๐Ÿ‡น"
  },
  {
    "name": "Honduras",
    "code": "HN",
    "phoneCode": "+504",
    "emoji": "๐Ÿ‡ญ๐Ÿ‡ณ"
  },
  {
    "name": "Hungary",
    "code": "HU",
    "phoneCode": "+36",
    "emoji": "๐Ÿ‡ญ๐Ÿ‡บ"
  },
  {
    "name": "Iceland",
    "code": "IS",
    "phoneCode": "+354",
    "emoji": "๐Ÿ‡ฎ๐Ÿ‡ธ"
  },
  {
    "name": "India",
    "code": "IN",
    "phoneCode": "+91",
    "emoji": "๐Ÿ‡ฎ๐Ÿ‡ณ"
  },
  {
    "name": "Indonesia",
    "code": "ID",
    "phoneCode": "+62",
    "emoji": "๐Ÿ‡ฎ๐Ÿ‡ฉ"
  },
  {
    "name": "Iran",
    "code": "IR",
    "phoneCode": "+98",
    "emoji": "๐Ÿ‡ฎ๐Ÿ‡ท"
  },
  {
    "name": "Iraq",
    "code": "IQ",
    "phoneCode": "+964",
    "emoji": "๐Ÿ‡ฎ๐Ÿ‡ถ"
  },
  {
    "name": "Ireland",
    "code": "IE",
    "phoneCode": "+353",
    "emoji": "๐Ÿ‡ฎ๐Ÿ‡ช"
  },
  {
    "name": "Israel",
    "code": "IL",
    "phoneCode": "+972",
    "emoji": "๐Ÿ‡ฎ๐Ÿ‡ฑ"
  },
  {
    "name": "Italy",
    "code": "IT",
    "phoneCode": "+39",
    "emoji": "๐Ÿ‡ฎ๐Ÿ‡น"
  },
  {
    "name": "Jamaica",
    "code": "JM",
    "phoneCode": "+1",
    "emoji": "๐Ÿ‡ฏ๐Ÿ‡ฒ"
  },
  {
    "name": "Japan",
    "code": "JP",
    "phoneCode": "+81",
    "emoji": "๐Ÿ‡ฏ๐Ÿ‡ต"
  },
  {
    "name": "Jordan",
    "code": "JO",
    "phoneCode": "+962",
    "emoji": "๐Ÿ‡ฏ๐Ÿ‡ด"
  },
  {
    "name": "Kazakhstan",
    "code": "KZ",
    "phoneCode": "+7",
    "emoji": "๐Ÿ‡ฐ๐Ÿ‡ฟ"
  },
  {
    "name": "Kenya",
    "code": "KE",
    "phoneCode": "+254",
    "emoji": "๐Ÿ‡ฐ๐Ÿ‡ช"
  },
  {
    "name": "Kiribati",
    "code": "KI",
    "phoneCode": "+686",
    "emoji": "๐Ÿ‡ฐ๐Ÿ‡ฎ"
  },
  {
    "name": "Kuwait",
    "code": "KW",
    "phoneCode": "+965",
    "emoji": "๐Ÿ‡ฐ๐Ÿ‡ผ"
  },
  {
    "name": "Kyrgyzstan",
    "code": "KG",
    "phoneCode": "+996",
    "emoji": "๐Ÿ‡ฐ๐Ÿ‡ฌ"
  },
  {
    "name": "Laos",
    "code": "LA",
    "phoneCode": "+856",
    "emoji": "๐Ÿ‡ฑ๐Ÿ‡ฆ"
  },
  {
    "name": "Latvia",
    "code": "LV",
    "phoneCode": "+371",
    "emoji": "๐Ÿ‡ฑ๐Ÿ‡ป"
  },
  {
    "name": "Lebanon",
    "code": "LB",
    "phoneCode": "+961",
    "emoji": "๐Ÿ‡ฑ๐Ÿ‡ง"
  },
  {
    "name": "Lesotho",
    "code": "LS",
    "phoneCode": "+266",
    "emoji": "๐Ÿ‡ฑ๐Ÿ‡ธ"
  },
  {
    "name": "Liberia",
    "code": "LR",
    "phoneCode": "+231",
    "emoji": "๐Ÿ‡ฑ๐Ÿ‡ท"
  },
  {
    "name": "Libya",
    "code": "LY",
    "phoneCode": "+218",
    "emoji": "๐Ÿ‡ฑ๐Ÿ‡พ"
  },
  {
    "name": "Liechtenstein",
    "code": "LI",
    "phoneCode": "+423",
    "emoji": "๐Ÿ‡ฑ๐Ÿ‡ฎ"
  },
  {
    "name": "Lithuania",
    "code": "LT",
    "phoneCode": "+370",
    "emoji": "๐Ÿ‡ฑ๐Ÿ‡น"
  },
  {
    "name": "Luxembourg",
    "code": "LU",
    "phoneCode": "+352",
    "emoji": "๐Ÿ‡ฑ๐Ÿ‡บ"
  },
  {
    "name": "Macedonia",
    "code": "MK",
    "phoneCode": "+389",
    "emoji": "๐Ÿ‡ฒ๐Ÿ‡ฐ"
  },
  {
    "name": "Madagascar",
    "code": "MG",
    "phoneCode": "+261",
    "emoji": "๐Ÿ‡ฒ๐Ÿ‡ฌ"
  },
  {
    "name": "Malawi",
    "code": "MW",
    "phoneCode": "+265",
    "emoji": "๐Ÿ‡ฒ๐Ÿ‡ผ"
  },
  {
    "name": "Malaysia",
    "code": "MY",
    "phoneCode": "+60",
    "emoji": "๐Ÿ‡ฒ๐Ÿ‡พ"
  },
  {
    "name": "Maldives",
    "code": "MV",
    "phoneCode": "+960",
    "emoji": "๐Ÿ‡ฒ๐Ÿ‡ป"
  },
  {
    "name": "Mali",
    "code": "ML",
    "phoneCode": "+223",
    "emoji": "๐Ÿ‡ฒ๐Ÿ‡ฑ"
  },
  {
    "name": "Malta",
    "code": "MT",
    "phoneCode": "+356",
    "emoji": "๐Ÿ‡ฒ๐Ÿ‡น"
  },
  {
    "name": "Marshall Islands",
    "code": "MH",
    "phoneCode": "+692",
    "emoji": "๐Ÿ‡ฒ๐Ÿ‡ญ"
  },
  {
    "name": "Mauritania",
    "code": "MR",
    "phoneCode": "+222",
    "emoji": "๐Ÿ‡ฒ๐Ÿ‡ท"
  },
  {
    "name": "Mauritius",
    "code": "MU",
    "phoneCode": "+230",
    "emoji": "๐Ÿ‡ฒ๐Ÿ‡บ"
  },
  {
    "name": "Mexico",
    "code": "MX",
    "phoneCode": "+52",
    "emoji": "๐Ÿ‡ฒ๐Ÿ‡ฝ"
  },
  {
    "name": "Micronesia",
    "code": "FM",
    "phoneCode": "+691",
    "emoji": "๐Ÿ‡ซ๐Ÿ‡ฒ"
  },
  {
    "name": "Moldova",
    "code": "MD",
    "phoneCode": "+373",
    "emoji": "๐Ÿ‡ฒ๐Ÿ‡ฉ"
  },
  {
    "name": "Monaco",
    "code": "MC",
    "phoneCode": "+377",
    "emoji": "๐Ÿ‡ฒ๐Ÿ‡จ"
  },
  {
    "name": "Mongolia",
    "code": "MN",
    "phoneCode": "+976",
    "emoji": "๐Ÿ‡ฒ๐Ÿ‡ณ"
  },
  {
    "name": "Montenegro",
    "code": "ME",
    "phoneCode": "+382",
    "emoji": "๐Ÿ‡ฒ๐Ÿ‡ช"
  },
  {
    "name": "Morocco",
    "code": "MA",
    "phoneCode": "+212",
    "emoji": "๐Ÿ‡ฒ๐Ÿ‡ฆ"
  },
  {
    "name": "Mozambique",
    "code": "MZ",
    "phoneCode": "+258",
    "emoji": "๐Ÿ‡ฒ๐Ÿ‡ฟ"
  },
  {
    "name": "Myanmar",
    "code": "MM",
    "phoneCode": "+95",
    "emoji": "๐Ÿ‡ฒ๐Ÿ‡ฒ"
  },
  {
    "name": "Namibia",
    "code": "NA",
    "phoneCode": "+264",
    "emoji": "๐Ÿ‡ณ๐Ÿ‡ฆ"
  },
  {
    "name": "Nauru",
    "code": "NR",
    "phoneCode": "+674",
    "emoji": "๐Ÿ‡ณ๐Ÿ‡ท"
  },
  {
    "name": "Nepal",
    "code": "NP",
    "phoneCode": "+977",
    "emoji": "๐Ÿ‡ณ๐Ÿ‡ต"
  },
  {
    "name": "Netherlands",
    "code": "NL",
    "phoneCode": "+31",
    "emoji": "๐Ÿ‡ณ๐Ÿ‡ฑ"
  },
  {
    "name": "New Zealand",
    "code": "NZ",
    "phoneCode": "+64",
    "emoji": "๐Ÿ‡ณ๐Ÿ‡ฟ"
  },
  {
    "name": "Nicaragua",
    "code": "NI",
    "phoneCode": "+505",
    "emoji": "๐Ÿ‡ณ๐Ÿ‡ฎ"
  },
  {
    "name": "Niger",
    "code": "NE",
    "phoneCode": "+227",
    "emoji": "๐Ÿ‡ณ๐Ÿ‡ช"
  },
  {
    "name": "Nigeria",
    "code": "NG",
    "phoneCode": "+234",
    "emoji": "๐Ÿ‡ณ๐Ÿ‡ฌ"
  },
  {
    "name": "North Korea",
    "code": "KP",
    "phoneCode": "+850",
    "emoji": "๐Ÿ‡ฐ๐Ÿ‡ต"
  },
  {
    "name": "Norway",
    "code": "NO",
    "phoneCode": "+47",
    "emoji": "๐Ÿ‡ณ๐Ÿ‡ด"
  },
  {
    "name": "Oman",
    "code": "OM",
    "phoneCode": "+968",
    "emoji": "๐Ÿ‡ด๐Ÿ‡ฒ"
  },
  {
    "name": "Pakistan",
    "code": "PK",
    "phoneCode": "+92",
    "emoji": "๐Ÿ‡ต๐Ÿ‡ฐ"
  },
  {
    "name": "Palau",
    "code": "PW",
    "phoneCode": "+680",
    "emoji": "๐Ÿ‡ต๐Ÿ‡ผ"
  },
  {
    "name": "Palestine",
    "code": "PS",
    "phoneCode": "+970",
    "emoji": "๐Ÿ‡ต๐Ÿ‡ธ"
  },
  {
    "name": "Panama",
    "code": "PA",
    "phoneCode": "+507",
    "emoji": "๐Ÿ‡ต๐Ÿ‡ฆ"
  },
  {
    "name": "Papua New Guinea",
    "code": "PG",
    "phoneCode": "+675",
    "emoji": "๐Ÿ‡ต๐Ÿ‡ฌ"
  },
  {
    "name": "Paraguay",
    "code": "PY",
    "phoneCode": "+595",
    "emoji": "๐Ÿ‡ต๐Ÿ‡พ"
  },
  {
    "name": "Peru",
    "code": "PE",
    "phoneCode": "+51",
    "emoji": "๐Ÿ‡ต๐Ÿ‡ช"
  },
  {
    "name": "Philippines",
    "code": "PH",
    "phoneCode": "+63",
    "emoji": "๐Ÿ‡ต๐Ÿ‡ญ"
  },
  {
    "name": "Poland",
    "code": "PL",
    "phoneCode": "+48",
    "emoji": "๐Ÿ‡ต๐Ÿ‡ฑ"
  },
  {
    "name": "Portugal",
    "code": "PT",
    "phoneCode": "+351",
    "emoji": "๐Ÿ‡ต๐Ÿ‡น"
  },
  {
    "name": "Qatar",
    "code": "QA",
    "phoneCode": "+974",
    "emoji": "๐Ÿ‡ถ๐Ÿ‡ฆ"
  },
  {
    "name": "Republic of Kosovo",
    "code": "XK",
    "phoneCode": "+383",
    "emoji": "๐Ÿ‡ฝ๐Ÿ‡ฐ"
  },
  {
    "name": "Romania",
    "code": "RO",
    "phoneCode": "+40",
    "emoji": "๐Ÿ‡ท๐Ÿ‡ด"
  },
  {
    "name": "Russia",
    "code": "RU",
    "phoneCode": "+7",
    "emoji": "๐Ÿ‡ท๐Ÿ‡บ"
  },
  {
    "name": "Rwanda",
    "code": "RW",
    "phoneCode": "+250",
    "emoji": "๐Ÿ‡ท๐Ÿ‡ผ"
  },
  {
    "name": "Saint Kitts and Nevis",
    "code": "KN",
    "phoneCode": "+1",
    "emoji": "๐Ÿ‡ฐ๐Ÿ‡ณ"
  },
  {
    "name": "Saint Lucia",
    "code": "LC",
    "phoneCode": "+1",
    "emoji": "๐Ÿ‡ฑ๐Ÿ‡จ"
  },
  {
    "name": "Saint Vincent and the Grenadines",
    "code": "VC",
    "phoneCode": "+1",
    "emoji": "๐Ÿ‡ป๐Ÿ‡จ"
  },
  {
    "name": "Samoa",
    "code": "WS",
    "phoneCode": "+685",
    "emoji": "๐Ÿ‡ผ๐Ÿ‡ธ"
  },
  {
    "name": "San Marino",
    "code": "SM",
    "phoneCode": "+378",
    "emoji": "๐Ÿ‡ธ๐Ÿ‡ฒ"
  },
  {
    "name": "Sรฃo Tomรฉ and Prรญncipe",
    "code": "ST",
    "phoneCode": "+239",
    "emoji": "๐Ÿ‡ธ๐Ÿ‡น"
  },
  {
    "name": "Saudi Arabia",
    "code": "SA",
    "phoneCode": "+966",
    "emoji": "๐Ÿ‡ธ๐Ÿ‡ฆ"
  },
  {
    "name": "Senegal",
    "code": "SN",
    "phoneCode": "+221",
    "emoji": "๐Ÿ‡ธ๐Ÿ‡ณ"
  },
  {
    "name": "Serbia",
    "code": "RS",
    "phoneCode": "+381",
    "emoji": "๐Ÿ‡ท๐Ÿ‡ธ"
  },
  {
    "name": "Seychelles",
    "code": "SC",
    "phoneCode": "+248",
    "emoji": "๐Ÿ‡ธ๐Ÿ‡จ"
  },
  {
    "name": "Sierra Leone",
    "code": "SL",
    "phoneCode": "+232",
    "emoji": "๐Ÿ‡ธ๐Ÿ‡ฑ"
  },
  {
    "name": "Singapore",
    "code": "SG",
    "phoneCode": "+65",
    "emoji": "๐Ÿ‡ธ๐Ÿ‡ฌ"
  },
  {
    "name": "Slovakia",
    "code": "SK",
    "phoneCode": "+421",
    "emoji": "๐Ÿ‡ธ๐Ÿ‡ฐ"
  },
  {
    "name": "Slovenia",
    "code": "SI",
    "phoneCode": "+386",
    "emoji": "๐Ÿ‡ธ๐Ÿ‡ฎ"
  },
  {
    "name": "Solomon Islands",
    "code": "SB",
    "phoneCode": "+677",
    "emoji": "๐Ÿ‡ธ๐Ÿ‡ง"
  },
  {
    "name": "Somalia",
    "code": "SO",
    "phoneCode": "+252",
    "emoji": "๐Ÿ‡ธ๐Ÿ‡ด"
  },
  {
    "name": "South Africa",
    "code": "ZA",
    "phoneCode": "+27",
    "emoji": "๐Ÿ‡ฟ๐Ÿ‡ฆ"
  },
  {
    "name": "South Korea",
    "code": "KR",
    "phoneCode": "+82",
    "emoji": "๐Ÿ‡ฐ๐Ÿ‡ท"
  },
  {
    "name": "South Sudan",
    "code": "SS",
    "phoneCode": "+211",
    "emoji": "๐Ÿ‡ธ๐Ÿ‡ธ"
  },
  {
    "name": "Spain",
    "code": "ES",
    "phoneCode": "+34",
    "emoji": "๐Ÿ‡ช๐Ÿ‡ธ"
  },
  {
    "name": "Sri Lanka",
    "code": "LK",
    "phoneCode": "+94",
    "emoji": "๐Ÿ‡ฑ๐Ÿ‡ฐ"
  },
  {
    "name": "Sudan",
    "code": "SD",
    "phoneCode": "+249",
    "emoji": "๐Ÿ‡ธ๐Ÿ‡ฉ"
  },
  {
    "name": "Suriname",
    "code": "SR",
    "phoneCode": "+597",
    "emoji": "๐Ÿ‡ธ๐Ÿ‡ท"
  },
  {
    "name": "Swaziland",
    "code": "SZ",
    "phoneCode": "+268",
    "emoji": "๐Ÿ‡ธ๐Ÿ‡ฟ"
  },
  {
    "name": "Sweden",
    "code": "SE",
    "phoneCode": "+46",
    "emoji": "๐Ÿ‡ธ๐Ÿ‡ช"
  },
  {
    "name": "Switzerland",
    "code": "CH",
    "phoneCode": "+41",
    "emoji": "๐Ÿ‡จ๐Ÿ‡ญ"
  },
  {
    "name": "Syria",
    "code": "SY",
    "phoneCode": "+963",
    "emoji": "๐Ÿ‡ธ๐Ÿ‡พ"
  },
  {
    "name": "Taiwan",
    "code": "TW",
    "phoneCode": "+886",
    "emoji": "๐Ÿ‡น๐Ÿ‡ผ"
  },
  {
    "name": "Tajikistan",
    "code": "TJ",
    "phoneCode": "+992",
    "emoji": "๐Ÿ‡น๐Ÿ‡ฏ"
  },
  {
    "name": "Tanzania",
    "code": "TZ",
    "phoneCode": "+255",
    "emoji": "๐Ÿ‡น๐Ÿ‡ฟ"
  },
  {
    "name": "Thailand",
    "code": "TH",
    "phoneCode": "+66",
    "emoji": "๐Ÿ‡น๐Ÿ‡ญ"
  },
  {
    "name": "Timor-Leste",
    "code": "TL",
    "phoneCode": "+670",
    "emoji": "๐Ÿ‡น๐Ÿ‡ฑ"
  },
  {
    "name": "Togo",
    "code": "TG",
    "phoneCode": "+228",
    "emoji": "๐Ÿ‡น๐Ÿ‡ฌ"
  },
  {
    "name": "Tonga",
    "code": "TO",
    "phoneCode": "+676",
    "emoji": "๐Ÿ‡น๐Ÿ‡ด"
  },
  {
    "name": "Trinidad and Tobago",
    "code": "TT",
    "phoneCode": "+1",
    "emoji": "๐Ÿ‡น๐Ÿ‡น"
  },
  {
    "name": "Tunisia",
    "code": "TN",
    "phoneCode": "+216",
    "emoji": "๐Ÿ‡น๐Ÿ‡ณ"
  },
  {
    "name": "Turkey",
    "code": "TR",
    "phoneCode": "+90",
    "emoji": "๐Ÿ‡น๐Ÿ‡ท"
  },
  {
    "name": "Turkmenistan",
    "code": "TM",
    "phoneCode": "+993",
    "emoji": "๐Ÿ‡น๐Ÿ‡ฒ"
  },
  {
    "name": "Tuvalu",
    "code": "TV",
    "phoneCode": "+688",
    "emoji": "๐Ÿ‡น๐Ÿ‡ป"
  },
  {
    "name": "Uganda",
    "code": "UG",
    "phoneCode": "+256",
    "emoji": "๐Ÿ‡บ๐Ÿ‡ฌ"
  },
  {
    "name": "Ukraine",
    "code": "UA",
    "phoneCode": "+380",
    "emoji": "๐Ÿ‡บ๐Ÿ‡ฆ"
  },
  {
    "name": "United Arab Emirates",
    "code": "AE",
    "phoneCode": "+971",
    "emoji": "๐Ÿ‡ฆ๐Ÿ‡ช"
  },
  {
    "name": "United Kingdom",
    "code": "UK",
    "phoneCode": "+44",
    "emoji": "๐Ÿ‡ฌ๐Ÿ‡ง"
  },
  {
    "name": "United States",
    "code": "US",
    "phoneCode": "+1",
    "emoji": "๐Ÿ‡บ๐Ÿ‡ธ"
  },
  {
    "name": "Uruguay",
    "code": "UY",
    "phoneCode": "+598",
    "emoji": "๐Ÿ‡บ๐Ÿ‡พ"
  },
  {
    "name": "Uzbekistan",
    "code": "UZ",
    "phoneCode": "+998",
    "emoji": "๐Ÿ‡บ๐Ÿ‡ฟ"
  },
  {
    "name": "Vanuatu",
    "code": "VU",
    "phoneCode": "+678",
    "emoji": "๐Ÿ‡ป๐Ÿ‡บ"
  },
  {
    "name": "Vatican City",
    "code": "VA",
    "phoneCode": "+39",
    "emoji": "๐Ÿ‡ป๐Ÿ‡ฆ"
  },
  {
    "name": "Venezuela",
    "code": "VE",
    "phoneCode": "+58",
    "emoji": "๐Ÿ‡ป๐Ÿ‡ช"
  },
  {
    "name": "Vietnam",
    "code": "VN",
    "phoneCode": "+84",
    "emoji": "๐Ÿ‡ป๐Ÿ‡ณ"
  },
  {
    "name": "Yemen",
    "code": "YE",
    "phoneCode": "+967",
    "emoji": "๐Ÿ‡พ๐Ÿ‡ช"
  },
  {
    "name": "Zambia",
    "code": "ZM",
    "phoneCode": "+260",
    "emoji": "๐Ÿ‡ฟ๐Ÿ‡ฒ"
  },
  {
    "name": "Zimbabwe",
    "code": "ZW",
    "phoneCode": "+263",
    "emoji": "๐Ÿ‡ฟ๐Ÿ‡ผ"
  }
]

Step 3 : Create Country class

Create a new Java class Country.java to represent the Country model with fields for name, code, phoneCode, and emoji.

package com.example.myapplication;

import android.os.Parcel;
import android.os.Parcelable;

public class Country implements Parcelable {
    private String name;
    private String code;
    private String phoneCode;
    private String emoji;

    public Country(String name, String code, String phoneCode, String emoji) {
        this.name = name;
        this.code = code;
        this.phoneCode = phoneCode;
        this.emoji = emoji;
    }

    public String getName() {
        return name;
    }

    public String getCode() {
        return code;
    }

    public String getPhoneCode() {
        return phoneCode;
    }

    public String getEmoji() {
        return emoji;
    }

    // Implement Parcelable methods to allow passing Country objects between activities
    protected Country(Parcel in) {
        name = in.readString();
        code = in.readString();
        phoneCode = in.readString();
        emoji = in.readString();
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeString(code);
        dest.writeString(phoneCode);
        dest.writeString(emoji);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public static final Creator<Country> CREATOR = new Creator<Country>() {
        @Override
        public Country createFromParcel(Parcel in) {
            return new Country(in);
        }

        @Override
        public Country[] newArray(int size) {
            return new Country[size];
        }
    };
}

Step 4: Create XML layout for country picker dialog

Create XML layout file dialog_country_picker.xml inside the res/layout folder to define the layout for the country picker dialog. Add below code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <AutoCompleteTextView
        android:id="@+id/searchEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Search Country"
        android:inputType="text"
        android:maxLines="1" />

    <ListView
        android:id="@+id/countryListView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

Step 5: Implement the MainActivity class with code to display the country picker dialog

package com.example.myapplication;

import android.app.AlertDialog;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.emoji.bundled.BundledEmojiCompatConfig;
import androidx.emoji.text.EmojiCompat;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private List<Country> allCountries;
    private List<Country> filteredCountries;
    private CountryAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Initialize EmojiCompat with bundled font support (no need for a FontRequest)
        EmojiCompat.Config config = new BundledEmojiCompatConfig(this);
        EmojiCompat.init(config);

        allCountries = getAllCountries();
        filteredCountries = new ArrayList<>(allCountries);

        adapter = new CountryAdapter(filteredCountries);

        // Click listener for the "Select Country" button
        findViewById(R.id.selectCountryButton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showCountryPickerDialog();
            }
        });
    }

    AlertDialog dialog;

    // Method to show the country picker dialog
    private void showCountryPickerDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Select a Country");

        // Inflate the dialog view layout
        View dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_country_picker, null);
        AutoCompleteTextView searchEditText = dialogView.findViewById(R.id.searchEditText);
        ListView countryListView = dialogView.findViewById(R.id.countryListView);

        countryListView.setAdapter(adapter);

        // Click listener for the country list items
        countryListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                Country selectedCountry = filteredCountries.get(position);
                // Handle the selected country here (you can do whatever you want with it)
                // For example, you can display it in a TextView or save it to a variable.
                Toast.makeText(MainActivity.this,
                        "Selected Country: " + selectedCountry.getName()
                                + "\nCountry Code: " + selectedCountry.getCode()
                                + "\nPhone Code: " + selectedCountry.getPhoneCode(),
                        Toast.LENGTH_SHORT).show();

                // Close the dialog
                countryListView.clearFocus();
                searchEditText.clearFocus();
                dialog.dismiss();
            }
        });

        // TextWatcher for filtering countries based on user input
        searchEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                filterCountries(charSequence.toString());
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });

        builder.setView(dialogView);
        builder.setNegativeButton("Cancel", null);

        dialog = builder.create();
        dialog.show();

        // Clear the search bar before opening the dialog again
        searchEditText.setText("");
    }

    // Method to filter the countries based on the search query
    private void filterCountries(String searchText) {
        filteredCountries.clear();
        for (Country country : allCountries) {
            if (country.getName().toLowerCase().contains(searchText.toLowerCase())) {
                filteredCountries.add(country);
            }
        }
        adapter.notifyDataSetChanged();
    }

    // Method to get the list of all countries from a JSON file
    private List<Country> getAllCountries() {
        List<Country> countries = new ArrayList<>();
        try {
            InputStream inputStream = getAssets().open("countries.json");
            int size = inputStream.available();
            byte[] buffer = new byte[size];
            inputStream.read(buffer);
            inputStream.close();

            String json = new String(buffer, "UTF-8");
            Gson gson = new Gson();
            Type listType = new TypeToken<List<Country>>() {}.getType();
            countries = gson.fromJson(json, listType);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return countries;
    }

    // Custom ArrayAdapter for displaying countries in the ListView
    private class CountryAdapter extends ArrayAdapter<Country> {

        CountryAdapter(List<Country> countries) {
            super(MainActivity.this, android.R.layout.simple_list_item_1, countries);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(android.R.layout.simple_list_item_1, parent, false);
            }

            TextView textView = convertView.findViewById(android.R.id.text1);
            Country country = getItem(position);
            if (country != null) {
                // Use EmojiCompat to display the flag Emoji
                CharSequence emoji = EmojiCompat.get().process(country.getEmoji());
                String countryText = emoji.toString() + " " + country.getName();
                textView.setText(countryText);
            }
            return convertView;
        }
    }

}

Step 6: Creating the MainActivity XML layout i.e activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">

<Button
android:id="@+id/selectCountryButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Country"
android:layout_centerInParent="true" />

</RelativeLayout>

Implementation In Kotlin

please follow step 1 and 2 from above

Step 3 : Create Country class

Create a new Kotlin class Country.kt to represent the Country model with fields for name, code, phoneCode, and emoji.

package com.example.myapplication

import android.os.Parcel
import android.os.Parcelable

data class Country(val name: String?, val code: String?, val phoneCode: String?, val emoji: String?) : Parcelable {
    constructor(parcel: Parcel) : this(
        parcel.readString(),
        parcel.readString(),
        parcel.readString(),
        parcel.readString()
    )

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeString(name)
        parcel.writeString(code)
        parcel.writeString(phoneCode)
        parcel.writeString(emoji)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator<Country> {
        override fun createFromParcel(parcel: Parcel): Country {
            return Country(parcel)
        }

        override fun newArray(size: Int): Array<Country?> {
            return arrayOfNulls(size)
        }
    }
}

Step 4: Create XML layout for country picker dialog

Create XML layout file dialog_country_picker.xml inside the res/layout folder to define the layout for the country picker dialog. Add below code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <AutoCompleteTextView
        android:id="@+id/searchEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Search Country"
        android:inputType="text"
        android:maxLines="1" />
    <ListView
        android:id="@+id/countryListView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

Step 5: Implement the MainActivity class with code to display the country picker dialog

package com.example.myapplication

import android.app.AlertDialog
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.AutoCompleteTextView
import android.widget.ListView
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.emoji.bundled.BundledEmojiCompatConfig
import androidx.emoji.text.EmojiCompat
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import java.io.IOException
import java.io.InputStream
import java.lang.reflect.Type

class MainActivity : AppCompatActivity() {

    private lateinit var allCountries: List<Country>
    private lateinit var filteredCountries: MutableList<Country>
    private lateinit var adapter: CountryAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Initialize EmojiCompat with bundled font support (no need for a FontRequest)
        val config = BundledEmojiCompatConfig(this)
        EmojiCompat.init(config)

        allCountries = getAllCountries()
        filteredCountries = ArrayList(allCountries)
        adapter = CountryAdapter(filteredCountries)

        // Click listener for the "Select Country" button
        findViewById<View>(R.id.selectCountryButton).setOnClickListener {
            showCountryPickerDialog()
        }
    }

    private lateinit var dialog: AlertDialog

    // Method to show the country picker dialog
    private fun showCountryPickerDialog() {
        val builder = AlertDialog.Builder(this)
        builder.setTitle("Select a Country")

        // Inflate the dialog view layout
        val dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_country_picker, null)
        val searchEditText = dialogView.findViewById<AutoCompleteTextView>(R.id.searchEditText)
        val countryListView = dialogView.findViewById<ListView>(R.id.countryListView)
        countryListView.adapter = adapter

        // Click listener for the country list items
        countryListView.onItemClickListener =
            AdapterView.OnItemClickListener { _, _, position, _ ->
                val selectedCountry = filteredCountries[position]
                // Handle the selected country here (you can do whatever you want with it)
                // For example, you can display it in a TextView or save it to a variable.
                Toast.makeText(
                    this@MainActivity,
                    "Selected Country: ${selectedCountry.name}\nCountry Code: ${selectedCountry.code}\nPhone Code: ${selectedCountry.phoneCode}",
                    Toast.LENGTH_SHORT
                ).show()

                // Close the dialog
                countryListView.clearFocus()
                searchEditText.clearFocus()
                dialog.dismiss()
            }

        // TextWatcher for filtering countries based on user input
        searchEditText.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {}

            override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {
                filterCountries(charSequence.toString())
            }

            override fun afterTextChanged(editable: Editable) {}
        })

        builder.setView(dialogView)
        builder.setNegativeButton("Cancel", null)

        dialog = builder.create()
        dialog.show()

        // Clear the search bar before opening the dialog again
        searchEditText.setText("")
    }

    // Method to filter the countries based on the search query
    private fun filterCountries(searchText: String) {
        filteredCountries.clear()
        for (country in allCountries) {
            if (country.name?.toLowerCase()?.contains(searchText.toLowerCase()) == true) {
                filteredCountries.add(country)
            }
        }
        adapter.notifyDataSetChanged()
    }

    // Method to get the list of all countries from a JSON file
    private fun getAllCountries(): List<Country> {
        var countries: List<Country> = ArrayList()
        try {
            val inputStream: InputStream = assets.open("countries.json")
            val size: Int = inputStream.available()
            val buffer = ByteArray(size)
            inputStream.read(buffer)
            inputStream.close()

            val json = String(buffer, Charsets.UTF_8)
            val gson = Gson()
            val listType: Type = object : TypeToken<List<Country?>?>() {}.type
            countries = gson.fromJson(json, listType)
        } catch (e: IOException) {
            e.printStackTrace()
        }
        return countries
    }

    // Custom ArrayAdapter for displaying countries in the ListView
    private inner class CountryAdapter(countries: List<Country>) :
        ArrayAdapter<Country>(this@MainActivity, android.R.layout.simple_list_item_1, countries) {

        override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
            var view = convertView
            if (view == null) {
                view = LayoutInflater.from(context)
                    .inflate(android.R.layout.simple_list_item_1, parent, false)
            }

            val textView = view!!.findViewById<TextView>(android.R.id.text1)
            val country = getItem(position)
            if (country != null) {
                // Use EmojiCompat to display the flag Emoji
                val emoji = EmojiCompat.get().process(country.emoji ?: "")
                val countryText = emoji.toString() + " " + country.name
                textView.text = countryText
            }
            return view
        }
    }
}

Step 6: Creating the MainActivity XML layout i.e activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">

    <Button
        android:id="@+id/selectCountryButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Select Country"
        android:layout_centerInParent="true" />

</RelativeLayout>

Conclusion

In conclusion, we have created a simple country picker in Android (java/kotlin) that allows the user to search for countries and select one from a list displayed in an alert dialog. The country data is stored in a JSON file located in the “assets” directory, and we use the Gson library to parse the JSON and create a list of country objects.

Please Subscribe Youtube| Like Facebook | Follow Twitter


Leave a Reply

Your email address will not be published. Required fields are marked *