{"id":107,"date":"2014-10-10T19:39:30","date_gmt":"2014-10-11T02:39:30","guid":{"rendered":"http:\/\/appinventor.pevest.com\/?p=107"},"modified":"2014-10-10T19:39:30","modified_gmt":"2014-10-11T02:39:30","slug":"using-listpicker-for-displaying-and-selecting-from-on-screen-lists","status":"publish","type":"post","link":"https:\/\/coldstreams.com\/appinventor\/2014\/10\/10\/using-listpicker-for-displaying-and-selecting-from-on-screen-lists\/","title":{"rendered":"Using ListPicker for displaying and selecting from on screen lists"},"content":{"rendered":"<p><strong>What is ListPicker?<\/strong><\/p>\n<p><em>ListPicker<\/em> is a user interface component that makes it easy to display a list of items and have the user make a selection from the list. For example, let&#8217;s say we&#8217;d like to display six things on the screen and then have the user select an item from the list.<\/p>\n<p>For simplicity, here is a sample <em>ListPicker<\/em> list showing six items labeled &#8220;A&#8221;, &#8220;B&#8221;, &#8220;C&#8221;, &#8220;D&#8221;, &#8220;E&#8221; and &#8220;F&#8221;. \u00a0Those items are arbitrary &#8211; your list could contain &#8220;Oranges&#8221;, &#8220;Bananas&#8221;, &#8220;Apples&#8221;, &#8220;Grapes&#8221; or whatever other text descriptions you would like to have. (The default color is white text on black but the color properties, as well as the text size, may be changed in the App Inventor Designer.)<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/Screenshot_2014-10-10-12-03-13.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-108\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/Screenshot_2014-10-10-12-03-13-576x1024.png\" alt=\"Screenshot_2014-10-10-12-03-13\" width=\"350\" height=\"622\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>When this screen is displayed the user may touch an item in the list to select it.<\/p>\n<p>Use <em>ListPicker<\/em> whenever you have a set of items from which the user is expected to make a selection. Example applications could include selecting a product from a list of inventory items, select a food item from a menu, select a name from a display of names and addresses and many more.<\/p>\n<p>Learning how to use <em>ListPicker<\/em> is helpful for new App Inventor programmers as many online tutorials and example programs use <em>ListPicker<\/em> &#8211; but without explaining what it is or how to use it! \u00a0Becoming familiar with\u00a0<em>ListPicker<\/em> will make those other tutorials easier to understand!<\/p>\n<p><strong>Example<br \/>\n<\/strong><\/p>\n<p>When an item is selected an event is created, and your event handler can then identify which item was selected in this list. \u00a0To illustrate, let&#8217;s start with a simple app that displays a list of items when the &#8220;Click me to display a short list&#8221; is pressed:<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/Screenshot_2014-10-10-12-03-24.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-109\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/Screenshot_2014-10-10-12-03-24-576x1024.png\" alt=\"Screenshot_2014-10-10-12-03-24\" width=\"300\" height=\"533\" \/><\/a><\/p>\n<p><em>ListPicker<\/em> does all the hard work for us &#8211; display the items on screen (as shown in the previous screen snap shot) and processing the selected element.<\/p>\n<p>In the example above, &#8220;C&#8221; was selected.<\/p>\n<p><strong>How to Build the ListPicker Demonstration\u00a0Application<\/strong><\/p>\n<p>Step 1 is to drag a <em>ListPicker<\/em> component from the Palette to the Viewer. Rename the control if you wish and change the text of the <em>ListPicker<\/em> control button to something useful \ud83d\ude42 Here is the app after dragging the <em>ListPicker<\/em> component and changing the text (in the <em>ListPicker<\/em> properties) to &#8220;Click me to display a short list&#8221;:<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/LP-Designer.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter  wp-image-110\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/LP-Designer-1024x734.png\" alt=\"LP-Designer\" width=\"460\" height=\"330\" \/><\/a><\/p>\n<p>Step 2 is to place some items into the list. We do that by typing a comma separated list in the <em>ElementsFromString<\/em> property\u00a0of <em>ListPicker<\/em>, as\u00a0shown here:<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/LP-Properties.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter  wp-image-111\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/LP-Properties.png\" alt=\"LP-Properties\" width=\"292\" height=\"317\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>We have now added six items &#8211; A, B, C, D, E and F. We could enter longer text descriptions\u00a0if we wished &#8211; for example &#8220;Oranges, Bananas,Apples&#8221; and so on:<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/LP-Properties2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter  wp-image-112\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/LP-Properties2.png\" alt=\"LP-Properties2\" width=\"280\" height=\"270\" \/><\/a><\/p>\n<p>Step 3 is to add an event handler for the list to process the list selection &#8211; this turns out to be really super hard &#8211; not! It is actually super easy! Click on the <em>ListPicker<\/em> component in the Blocks Editor and then drag the <em>.AfterPicking<\/em> event into your program:<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/LP-EventBlock.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-113\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/LP-EventBlock.png\" alt=\"LP-EventBlock\" width=\"988\" height=\"166\" \/><\/a><em>ListPicker<\/em> handles all the details of the on screen selection. All that is needed is to fetch the item that was selected, store that in a variable, or, as in this example, display the selection in a label field on the app&#8217;s screen.<\/p>\n<p>That&#8217;s it for the basic operation of the <em>ListPicker<\/em>. If you have many elements &#8211; too many to fit on one screen &#8211; the list automatically handles scrolling. Just use your finger to drag up or down the list. You can also change <em>ListPicker<\/em> properties such as the color of the list and more.<\/p>\n<p>Of course there are many <em>ListPicker<\/em>\u00a0features, but this is intended as a brief introduction.<\/p>\n<p>But one feature we wish to mention is that you can add items to the list to display from within your program by putting your items in a list and assigning them as shown here:<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/LP-BlocksAdding.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-114\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/LP-BlocksAdding.png\" alt=\"LP-BlocksAdding\" width=\"1024\" height=\"386\" \/><\/a>The <em>ListPicker1.Elements<\/em> property contains a programmatic list of items. \u00a0If you are not familiar with lists, please see Chapter 8 &#8220;Introduction to Lists&#8221; in my e-book &#8220;Guide to App Inventor 2:Tutorial &#8211; The fast and easy way to create Android apps&#8221;.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is ListPicker? ListPicker is a user interface component that makes it easy to display a list of items and have the user make a selection from the list. For example, let&#8217;s say we&#8217;d like to display six things on the screen and then have the user select an item from the list. For simplicity, &hellip; <a href=\"https:\/\/coldstreams.com\/appinventor\/2014\/10\/10\/using-listpicker-for-displaying-and-selecting-from-on-screen-lists\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Using ListPicker for displaying and selecting from on screen lists<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,13],"tags":[],"class_list":["post-107","post","type-post","status-publish","format-standard","hentry","category-components","category-tips"],"_links":{"self":[{"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/posts\/107","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/comments?post=107"}],"version-history":[{"count":0,"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/posts\/107\/revisions"}],"wp:attachment":[{"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/media?parent=107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/categories?post=107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/tags?post=107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}