{"id":1242,"date":"2016-05-02T12:32:42","date_gmt":"2016-05-02T19:32:42","guid":{"rendered":"http:\/\/appinventor.pevest.com\/?p=1242"},"modified":"2016-05-02T12:32:42","modified_gmt":"2016-05-02T19:32:42","slug":"aligning-the-text-that-appears-in-listpicker","status":"publish","type":"post","link":"https:\/\/coldstreams.com\/appinventor\/2016\/05\/02\/aligning-the-text-that-appears-in-listpicker\/","title":{"rendered":"Aligning the text that appears in ListPicker"},"content":{"rendered":"<p>Readers post questions on the FB page or the blog. Sometimes I can answer them but sometimes I cannot answer them right away. For those that I cannot answer, I add the question to a list of future tutorial ideas. If someone is not sure how to solve a problem, chances are that there are\u00a0others who may need help with the same issue!<\/p>\n<p>I am beginning to go through my list &#8211; watch for more tutorials based on reader questions. Note &#8211; I do not have time to solve specific or custom applications. I try to abstract the basic elements of the problem and create a generic solution that can apply to a wide variety of use cases.<\/p>\n<h2>ListPicker Text Alignment<\/h2>\n<p>A reader asked how to align the text that appears in the <em>ListPicker<\/em> box. The <em>ListPicker<\/em> displays a set of items on screen so that the user may select an item from the list. When the list appears on screen, all the items are &#8220;left justified&#8221; which means they appear on the left side of the screen.<\/p>\n<p>To demonstrate, our <em>ListPicker, <\/em>below<em>,<\/em> displays a list of auto manufacturers:<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/05\/Screenshot_20160502-115325.png\" rel=\"attachment wp-att-1244\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-1244 aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/05\/Screenshot_20160502-115325-169x300.png\" alt=\"Screenshot_20160502-115325\" width=\"169\" height=\"300\" \/><\/a><em><strong>Is there a way to center or right justify the items that appear in the ListPicker list like this?<\/strong><\/em> The first 4 items in this list are right justified and the last two are centered:<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/05\/Screenshot_20160502-114442.png\" rel=\"attachment wp-att-1245\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-1245 aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/05\/Screenshot_20160502-114442-169x300.png\" alt=\"Screenshot_20160502-114442\" width=\"169\" height=\"300\" \/><\/a>The answer to that question is basically &#8220;yes&#8221;, but it may not be perfect &#8211; as we will see.<\/p>\n<p><!--more--><\/p>\n<h2>Designer View<\/h2>\n<p>In the Designer View, we drag a <em>ListPicker<\/em> component from the Palette and then set the properties for the <em>ListPicker<\/em>.<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/05\/DesignerView.png\" rel=\"attachment wp-att-1246\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1246 aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/05\/DesignerView.png\" alt=\"DesignerView\" width=\"339\" height=\"96\" \/><\/a>Our sample app displays the <em>ListPicker<\/em> button (the <em>ListPicker<\/em> looks like and acts like a button &#8211; when touched, the button launches the <em>ListPicker<\/em> selection list on a new screen).<\/p>\n<p>After selection, our app displays the item selected in the list in the are shown in the Designer View as &#8220;Your selection&#8221; &#8211; this is a <em>Label<\/em> component (whose name is set to <em>lblSelected<\/em>).<\/p>\n<h2>Calculating the Width<\/h2>\n<p>For centering and right alignment to work, we need to know the width\u00a0of the screen that will be used by the <em>ListPicker<\/em>. Unfortunately, there is no way to know the<i>\u00a0ListPicker<\/i>\u00a0width but we can approximate the width by setting the width of the <em>ListPicker<\/em> button to fill the entire screen, horizontally, and then reference the width of the button itself. Of course, this requires that the <em>ListPicker<\/em> button be used as a full width button.<\/p>\n<p>The button&#8217;s width is set in Properties by selecting the button and then setting its <em>Width<\/em> property to &#8220;Fill parent&#8221;:<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/05\/DesignerViewWidth.png\" rel=\"attachment wp-att-1247\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-1247 aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/05\/DesignerViewWidth.png\" alt=\"DesignerViewWidth\" width=\"183\" height=\"202\" \/><\/a>The\u00a0<em>ListPicker<\/em> button has a property called <em>TextAlignment<\/em> which sounds promising &#8211; but it only aligns the text that appears on the button itself and has nothing to do with the content of the <em>ListPicker<\/em> selection list:<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/05\/DesignerViewTextAlignment.png\" rel=\"attachment wp-att-1248\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1248 aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/05\/DesignerViewTextAlignment.png\" alt=\"DesignerViewTextAlignment\" width=\"144\" height=\"117\" \/><\/a>Blocks Code<\/p>\n<p>The trick to right justifying or centering the text requires us to estimate the width of the <em>ListPicker<\/em> selection list, and then to add spaces in front of each text item appearing in the list.<\/p>\n<p>To do this, we reference the property <em>ListPicker1.Width<\/em> and divide it by an estimated number of pixels per character \u00a0(I used 8) to estimate the width in terms of text characters. This value is stored in the global variable <em>ListPickerWidth<\/em>.<\/p>\n<p><em>ListPicker1.Width<\/em> is the width of the button itself and NOT the width of the full screen <em>ListPicker<\/em> selection list &#8211; this is why we needed to set the <em>ListPicker&#8217;s<\/em> Width property to &#8220;fill screen&#8221;.<\/p>\n<p>We add items into the selection list, aligning each item to the right &#8211; or to the center &#8211; by calling a couple of procedures &#8211; <em>AlignRight<\/em> and <em>AlignCenter<\/em> (see below).<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/05\/Blocks1.gif\" rel=\"attachment wp-att-1250\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-1250 aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/05\/Blocks1-1024x670.gif\" alt=\"Blocks1\" width=\"546\" height=\"357\" \/><\/a><em>lblSelected<\/em> refers to the <em>Label<\/em> component that will display the selected item. We initialize it to display the calculated <em>ListPickerWidth<\/em>.<\/p>\n<h2>Aligning the Text<\/h2>\n<p>To align the text to the right, we need to add spaces in front of the selection item. If our selection item is &#8220;Honda&#8221;, then to make it appear on the right side of the <em>ListPicker<\/em>, we insert spaces in front so that &#8220;Honda&#8221; becomes &#8221; \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 Honda&#8221;.<\/p>\n<p>There are several ways we can put spaces on the front of the text string. In the method here, we use the <em>segment text<\/em> block to extract a string of spaces from a long set of blank space characters.<\/p>\n<p>If the width is, say, 40, and the string is &#8220;Honda&#8221; (5 characters long), then we add 35 blank spaces in front of &#8220;Honda&#8221; to create a 40 character wide string for the selection list:<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/05\/BlocksAlignRight.gif\" rel=\"attachment wp-att-1251\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1251 aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/05\/BlocksAlignRight-1024x244.gif\" alt=\"BlocksAlignRight\" width=\"566\" height=\"135\" \/><\/a>In our example for &#8220;Honda&#8221; we extract 35 blank spaces from the text field containing the spaces and then join the word &#8220;Honda&#8221; on to the end, giving us:<\/p>\n<p>&#8221;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Honda&#8221;<\/p>\n<p>Note that the <em>X<\/em> variable is a parameter variable. When our code calls <em>AlignRight<\/em>, the value &#8220;Honda&#8221; was passed through to the procedure where it is stored in the variable <em>X<\/em>.<\/p>\n<p>Centering is handled in a similar fashion &#8211; except we add only half as many spaces:<a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/05\/BlocksAlignCenter.gif\" rel=\"attachment wp-att-1252\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1252 aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/05\/BlocksAlignCenter-1024x229.gif\" alt=\"BlocksAlignCenter\" width=\"554\" height=\"124\" \/><\/a>Make sure the string containing the blank spaces, above is long enough!<\/p>\n<p>Finally, we process the <em>ListPicker AfterPicking<\/em> event and display the selected item&#8217;s index (position in the list) and the text of the selected item, on the screen:<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/05\/BlocksAfterPicking.gif\" rel=\"attachment wp-att-1253\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1253 aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/05\/BlocksAfterPicking-1024x220.gif\" alt=\"BlocksAfterPicking\" width=\"532\" height=\"114\" \/><\/a><\/p>\n<p>If we selected &#8220;Chrysler&#8221;, then the index position of 4 and the text &#8220;Chrysler&#8221; are displayed on the screen.<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/05\/Screenshot_20160502-114810.png\" rel=\"attachment wp-att-1254\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1254 aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/05\/Screenshot_20160502-114810-576x1024.png\" alt=\"Screenshot_20160502-114810\" width=\"272\" height=\"483\" \/><\/a><\/p>\n<h2>Issues<\/h2>\n<p>Making this work required us to use a &#8220;work around&#8221; of setting the button width to the maximum width of the screen, and then use the button width to estimate the width of the <em>ListPicker<\/em> selection list. This value is not precise and results in the alignment being slightly off. It may be necessary to add a small constant to the estimated text width &#8211; but whether that would work on all devices would require additional testing.<\/p>\n<h2>Source Code<\/h2>\n<p>No source code link as there is very little source code in this example! Just enter the blocks you need for your own apps!<\/p>\n<h2>E-Books and Printed Books<\/h2>\n<p>If you find these tutorials helpful (I hope you do!) please take a look at my books on App Inventor. To learn more about the books and where to get them (they are inexpensive) please see my <a href=\"http:\/\/appinventor.pevest.com\/?page_id=33\">App Inventor Books page<\/a>.<\/p>\n<ul>\n<li><strong>App Inventor 2 Introduction (Volume 1 e-book)<\/strong><br \/>\nStep-by-step guide to easy Android programming<\/li>\n<li><strong>App Inventor 2 Advanced Concepts\u00a0(Volume\u00a02 e-book)<\/strong><br \/>\nStep-by-step guide\u00a0to\u00a0Advanced features including TinyDB<\/li>\n<li><strong>App Inventor 2 Databases and Files<\/strong>\u00a0<strong>(Volume 3 e-book)<\/strong><br \/>\nStep-by-step TinyDB, TinyWebDB, Fusion Tables and Files<\/li>\n<li><strong>App Inventor 2 Graphics, Animation and Charts (Volume 4 e-book)<\/strong><br \/>\nStep-by-step guide to graphics, animation and charts<\/li>\n<\/ul>\n<p>Thank you for visiting! &#8212; Ed<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Readers post questions on the FB page or the blog. Sometimes I can answer them but sometimes I cannot answer them right away. For those that I cannot answer, I add the question to a list of future tutorial ideas. If someone is not sure how to solve a problem, chances are that there are\u00a0others &hellip; <a href=\"https:\/\/coldstreams.com\/appinventor\/2016\/05\/02\/aligning-the-text-that-appears-in-listpicker\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Aligning the text that appears in ListPicker<\/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,10,1],"tags":[],"class_list":["post-1242","post","type-post","status-publish","format-standard","hentry","category-components","category-programming-method","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/posts\/1242","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=1242"}],"version-history":[{"count":0,"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/posts\/1242\/revisions"}],"wp:attachment":[{"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/media?parent=1242"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/categories?post=1242"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/tags?post=1242"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}