{"id":5401,"date":"2024-12-27T17:23:02","date_gmt":"2024-12-27T17:23:02","guid":{"rendered":"https:\/\/obsidiansofteducation.com\/?page_id=5401"},"modified":"2024-12-27T17:23:02","modified_gmt":"2024-12-27T17:23:02","slug":"country-city-dropdown-spinner-in-mit-app-inventor-coding-dependent-spinners","status":"publish","type":"page","link":"https:\/\/obsidiansofteducation.com\/index.php\/country-city-dropdown-spinner-in-mit-app-inventor-coding-dependent-spinners\/","title":{"rendered":"Country \u2013 City Dropdown (Spinner) in MIT App Inventor \u2013 Coding Dependent Spinners"},"content":{"rendered":"\n<p><br>Today I will show you how to code two dropdowns where the second one is dependent on the first one.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/obsidiansofteducation.com\/wp-content\/uploads\/2024\/12\/snake20game2037.png\" alt=\"\"\/><\/figure>\n<\/div>\n\n\n<p>In MIT app inventor, a dropdown is called a spinner.<\/p>\n\n\n\n<p>As a bonus, I will be coding a very practical example. I will be making a country and city dropdown where the city dropdown is dependent on which country is selected in the country dropdown. These are very much needed in apps where the user has to provide his city and country.<\/p>\n\n\n\n<p>We need country and city data for coding this project.<\/p>\n\n\n\n<p>I did a lot of research and wanted to find you a 100% free solution so I didn\u2019t go for API calls and downloaded JSON files from Git Hub that served our purpose. These are two of the JSON files containing country and city data according to our needs:<\/p>\n\n\n\n<p><a href=\"https:\/\/github.com\/russ666\/all-countries-and-cities-json\/blob\/master\/countries.json\" target=\"_blank\" rel=\"noopener\">https:\/\/github.com\/russ666\/all-countries-and-cities-json\/blob\/master\/countries.json<\/a><\/p>\n\n\n\n<p>OR<\/p>\n\n\n\n<p><a href=\"https:\/\/github.com\/David-Haim-zz\/CountriesToCitiesJSON\/blob\/master\/countriesToCities.json\" target=\"_blank\" rel=\"noopener\">https:\/\/github.com\/David-Haim-zz\/CountriesToCitiesJSON\/blob\/master\/countriesToCities.json<\/a><\/p>\n\n\n\n<p>Both have countries as keys with a list of cities as a value. You can open up the JSON file using a browser and go through it to see which one is more comprehensive for your app. You can also carefully edit them using an editor like Notepad or SublimeText.<\/p>\n\n\n\n<p>I am going to use russ666 one in this tutorial.<\/p>\n\n\n\n<p>Download the raw file. Open up a new project in MIT App Inventor and rename it to CountryCityDropdowns.<\/p>\n\n\n\n<p>Upload the downloaded countries.json file in the media section.<\/p>\n\n\n\n<p>Make align horizontal center for screen1<\/p>\n\n\n\n<p>Add a spinner from the user interface. Rename it to countrySpinner and change its width to 50%.<\/p>\n\n\n\n<p>Duplicate this spinner by selecting it and&nbsp;<strong>pressing Ctrl + C and Ctrl + V<\/strong>&nbsp;for Windows on the keyboard OR&nbsp;<strong>Command + C and Command + V<\/strong>&nbsp;for MacOS.<\/p>\n\n\n\n<p>Rename it to citySpinner.<\/p>\n\n\n\n<p>Add a file component from the storage palette. This is for reading the JSON file.<\/p>\n\n\n\n<p>Add a web component from connectivity. We will not be using any GET request as we have made the JSON file a part of our app but we still need the web component for decoding JSON.<\/p>\n\n\n\n<p>Go to the blocks section.<\/p>\n\n\n\n<p>Make global variables:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/obsidiansofteducation.com\/wp-content\/uploads\/2024\/12\/OCZLDVaGsZLsvUZododfCtRScT4QATCrXgpih39c3k73E_59PZ92-4NeoYqWB-mbujDHMiJBplVZCooSg61PVyi1uxeanz7CxuLHusvrPmUAil8D_LOwbONCm5rbrjYfOJfiqEYl_W0lTASZk6YnJIU.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>When the screen is initialized, we need to read our JSON file.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/obsidiansofteducation.com\/wp-content\/uploads\/2024\/12\/hDlOvqIMIEcc0vwqEbe5J4veYpRaejVlR14pCWAAzHkIRIpwkf5R5GZoWXC4vwFoe7leXmB2kEfX6EgNfDp_stK41EQxB-YmJ1MS6x4M60uBcUV8SjxIPTNhUoY9-RoeixWHvB71qo_rmmKar-SQeLQ.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>Make sure that the spelling is the same as the uploaded JSON file.<\/p>\n\n\n\n<p>File.readFrom will trigger the File\u2019s GotText event.<\/p>\n\n\n\n<p>In that, use the web component to decode JSON into our global dictionary variable i.e. data<\/p>\n\n\n\n<p>Remember that all keys in this dictionary are names of the countries so use getkeys to fill up the countries list. Add the \u201cSelect country\u201d item in the first place of this list and then, assign this list to the country spinner\u2019s elements.<\/p>\n\n\n\n<p>Set city spinner to just one element: Select City<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/obsidiansofteducation.com\/wp-content\/uploads\/2024\/12\/Bs477e0-pQp45XyipEXyTAUXeq3LNAUhjLdaxwIlR1zU5EaPkbNJZqpk9lqusMUFkUCgcFrr7rLOMjCApyAjbSzDuHWiTjcC039NoZ64gv2t6ax0QQZ00j-kf9uhRJWoNx2AEVuAc0NAufVuukqJdTo.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>When the user selects a country from the country spinner, the after-selecting event is triggered.<\/p>\n\n\n\n<p>Make a local city list variable and get the list of cities from our data dictionary against the key equal to the user\u2019s selection.&nbsp;<\/p>\n\n\n\n<p>Insert a Select City item in the first place in this list.<\/p>\n\n\n\n<p>Now, assign this list to the elements of the citySpinner and set the selection index to 1.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/obsidiansofteducation.com\/wp-content\/uploads\/2024\/12\/xu2Lw8jmSgOX5fiRQjO_Jq9QADp6NvcrbyA87tyFIt1K9_w0wJHGFOAxgExG2tBHjcgYxjktVwF3YfIfnfDb8OIp-R5naCyEIZQpeJPdB13g2xhZnWZQXWCOMevP4QGmgHbOOSY39f64m4TYRkUH_oQ.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>This is done. So, it is quite easy to code dependent spinners in MIT App Inventor and you also learned how to show countries and cities dropdowns in your app.<\/p>\n\n\n\n<p>I hope you liked this tutorial. Do share it with your friends and family.<\/p>\n\n\n\n<p>If you like my tutorials, consider supporting me:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter is-resized\"><a href=\"https:\/\/obsidiansoft.gumroad.com\/l\/cxdft\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"372\" height=\"196\" src=\"https:\/\/obsidiansofteducation.com\/wp-content\/uploads\/2024\/12\/donate-button-1.png\" alt=\"\" class=\"wp-image-5381\" style=\"width:231px;height:auto\" srcset=\"https:\/\/obsidiansofteducation.com\/wp-content\/uploads\/2024\/12\/donate-button-1.png 372w, https:\/\/obsidiansofteducation.com\/wp-content\/uploads\/2024\/12\/donate-button-1-300x158.png 300w\" sizes=\"auto, (max-width: 372px) 100vw, 372px\" \/><\/a><\/figure>\n<\/div>\n\n\n<p>You can also watch a video tutorial for it on my YouTube channel.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<p class=\"responsive-video-wrap clr\"><iframe loading=\"lazy\" title=\"Dependent Dropdown in MIT App Inventor | Country City Dropdowns | App Inventor Connected Dropdowns\" width=\"1200\" height=\"675\" src=\"https:\/\/www.youtube.com\/embed\/2kBRE3G_MLM?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<\/div><\/figure>\n\n\n\n<p>Also, if you haven\u2019t subscribed to my channel, kindly do so so that you don\u2019t miss any of the great projects that I have planned for you.<\/p>\n\n\n\n<p><a href=\"https:\/\/www.youtube.com\/c\/obsidiansofteducation\" target=\"_blank\" rel=\"noopener\">https:\/\/www.youtube.com\/c\/obsidiansofteducation<\/a><\/p>\n\n\n\n<p>Please like my social media pages for more educational resources and tips.<\/p>\n\n\n\n<p>Facebook:<a href=\"https:\/\/www.blogger.com\/blog\/page\/edit\/4947000819484484884\/7140650632339107612#\" target=\"_blank\" rel=\"noopener\">&nbsp;https:\/\/www.facebook.com\/ObsidianSoft\/<\/a><\/p>\n\n\n\n<p>Instagram:<a href=\"https:\/\/www.blogger.com\/blog\/page\/edit\/4947000819484484884\/7140650632339107612#\" target=\"_blank\" rel=\"noopener\">&nbsp;https:\/\/www.instagram.com\/obsidiansoftapps\/<\/a><\/p>\n\n\n\n<p>Pinterest: <a href=\"https:\/\/www.pinterest.com\/obsidiansoft\/\" target=\"_blank\" rel=\"noopener\">https:\/\/www.pinterest.com\/obsidiansoft\/<\/a><a href=\"\"><\/a><\/p>\n\n\n\n<p>For links to free educational apps, have a look at the\u00a0<a href=\"https:\/\/obsidiansofteducation.com\/index.php\/mobile-apps\/\">educational apps page<\/a><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today I will show you how to code two dropdowns where the second one is dependent on the first one. In MIT app inventor, a dropdown is called a spinner. As a bonus, I will be coding a very practical example. I will be making a country and city dropdown where the city dropdown is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-5401","page","type-page","status-publish","hentry","entry"],"_links":{"self":[{"href":"https:\/\/obsidiansofteducation.com\/index.php\/wp-json\/wp\/v2\/pages\/5401","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/obsidiansofteducation.com\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/obsidiansofteducation.com\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/obsidiansofteducation.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/obsidiansofteducation.com\/index.php\/wp-json\/wp\/v2\/comments?post=5401"}],"version-history":[{"count":1,"href":"https:\/\/obsidiansofteducation.com\/index.php\/wp-json\/wp\/v2\/pages\/5401\/revisions"}],"predecessor-version":[{"id":5402,"href":"https:\/\/obsidiansofteducation.com\/index.php\/wp-json\/wp\/v2\/pages\/5401\/revisions\/5402"}],"wp:attachment":[{"href":"https:\/\/obsidiansofteducation.com\/index.php\/wp-json\/wp\/v2\/media?parent=5401"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}