{"id":1129,"date":"2016-01-19T14:32:10","date_gmt":"2016-01-19T22:32:10","guid":{"rendered":"http:\/\/appinventor.pevest.com\/?p=1129"},"modified":"2016-01-19T14:32:10","modified_gmt":"2016-01-19T22:32:10","slug":"making-pretty-app-inventor-user-interface-controls","status":"publish","type":"post","link":"https:\/\/coldstreams.com\/appinventor\/2016\/01\/19\/making-pretty-app-inventor-user-interface-controls\/","title":{"rendered":"Making &#8220;pretty&#8221; App Inventor user interface controls"},"content":{"rendered":"<p>In\u00a0the real world, &#8220;user interfaces&#8221; look like electric light switches, push buttons or control knobs, temperature dials on ovens, volume controls on radios and so forth. We can mimic these types of controls for our touch screen Android apps. We do not have to rely on the boring desktop-like clickable button or checkboxes in the App Inventor user interface palette.<\/p>\n<p>A <a href=\"http:\/\/appinventor.pevest.com\/?p=385\">previous tutorial<\/a> showed a trick to make the color of a button vary continuously. This tutorial shows how to use our own images, instead of the boring button, together with a bit of code to bring these buttons\u00a0to life.<\/p>\n<h2>User Interface View<\/h2>\n<p>Below is a sample &#8220;toggle switch&#8221;. Press the switch icon once and the toggle moves to the right; press it again and the toggle moves back to the left.<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/01\/toggleswitch1.gif\" rel=\"attachment wp-att-1142\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1142 aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/01\/toggleswitch1-300x127.gif\" alt=\"toggleswitch1\" width=\"106\" height=\"45\" \/><\/a><\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/01\/toggleswitch2.gif\" rel=\"attachment wp-att-1143\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1143 aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/01\/toggleswitch2-300x127.gif\" alt=\"toggleswitch2\" width=\"106\" height=\"45\" \/><\/a><\/p>\n<p>Here is a slide switch. Press the slide switch once and the switch position moves to the right and the switch illuminates in green. Press the slide switch again and the switch returns to the left.<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/01\/switch1.gif\" rel=\"attachment wp-att-1144\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1144 aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/01\/switch1-300x92.gif\" alt=\"switch1\" width=\"147\" height=\"45\" \/><\/a><\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/01\/switch3.gif\" rel=\"attachment wp-att-1145\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1145 aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/01\/switch3-300x90.gif\" alt=\"switch3\" width=\"150\" height=\"45\" \/><\/a><\/p>\n<p>Here is a concept for a raised momentary push button. Pressing the button changes the appearance of the button\u00a0<em>while your finger is on the butto<\/em>n &#8211; to look like the button is pressed in.<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/01\/UI1.gif\" rel=\"attachment wp-att-1146\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1146 aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/01\/UI1.gif\" alt=\"UI1\" width=\"99\" height=\"77\" \/><\/a><\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/01\/UI2.gif\" rel=\"attachment wp-att-1147\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1147 aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/01\/UI2.gif\" alt=\"UI2\" width=\"99\" height=\"85\" \/><\/a><\/p>\n<h2>The Designer View<\/h2>\n<p><!--more--><\/p>\n<p>Creating the interface requires dragging the usual button control from the Palette into the Designer View area. But the normal button image is replaced with our custom graphics.<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/01\/Designer.gif\" rel=\"attachment wp-att-1141\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-1141 aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/01\/Designer.gif\" alt=\"Designer\" width=\"160\" height=\"460\" \/><\/a><\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/01\/Components.gif\" rel=\"attachment wp-att-1148\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1148 aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/01\/Components.gif\" alt=\"Components\" width=\"109\" height=\"122\" \/><\/a><\/p>\n<p>To set the graphic image, select the button in the Designer View. Then, in the Properties for\u00a0the button, look for <em>Image<\/em>\u00a0and click on that to display a pop up dialog box of options. The default image is &#8220;None&#8221;. Initially the\u00a0list of images is empty &#8211; select <em>Upload File<\/em> to locate your .jpg or .png graphic item to use for a button and upload the images to App Inventor.<\/p>\n<p>In this example, all of the images to be used in this demonstration app have already been uploaded.<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/01\/SetImage.gif\" rel=\"attachment wp-att-1149\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-1149 aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/01\/SetImage-204x300.gif\" alt=\"SetImage\" width=\"159\" height=\"234\" \/><\/a><\/p>\n<p>Below the Components area of the screen, you should see a box labeled &#8220;Media&#8221;, like this:<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/01\/Media.gif\" rel=\"attachment wp-att-1150\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1150 aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/01\/Media-300x291.gif\" alt=\"Media\" width=\"162\" height=\"157\" \/><\/a><\/p>\n<p>This box also displays all of the media files you have loaded in to your project. You may upload files using the <em>Upload File&#8230;<\/em> button; however, you will still need to select the uploaded file to set the image for your individual buttons.<\/p>\n<p>You may\u00a0notice that I have uploaded two graphics for each button. One graphic, say with the toggle switch, shows the button with the toggle on the left and a second graphic shows the toggle on the right. \u00a0When the toggle switch &#8220;button&#8221; is pressed, the event handler alternates the toggle switch graphic from the left to the right or right to left.<\/p>\n<h2>Blocks View<\/h2>\n<p>To enable the button images to flip back and forth requires some code to reset the button&#8217;s image property.<\/p>\n<p>For the toggle switch, we need a variable, <em>ToggleSwitch<\/em>, to keep track of which image is displayed. I have used a binary value that is either <em>true<\/em> or <em>false<\/em>. \u00a0A <em>true<\/em> value means the switch is presently to the left; a <em>false<\/em> value means the switch is to the right.<\/p>\n<p>In the button&#8217;s <em>Click<\/em> event handler, if <em>ToggleSwitch<\/em> is <em>true<\/em>, then we set the button&#8217;s image property to the graphic that shows the switch to the right and set the value of <em>ToggleSwitch<\/em> to <em>false<\/em>.<\/p>\n<p>Otherwise, we do the opposite &#8211; if <em>ToggleSwitch<\/em> is false (the <em>else<\/em> block below), then we set the image back to the one showing the toggle switch to the left and set the value of <em>ToggleSwitch<\/em> to true.<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/01\/ToggleSwitch.gif\" rel=\"attachment wp-att-1152\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1152 aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/01\/ToggleSwitch.gif\" alt=\"ToggleSwitch\" width=\"456\" height=\"234\" \/><\/a><\/p>\n<p>The <em>Slider Switch<\/em> is handled identically to\u00a0<em>ToggleSwitch<\/em> except we\u00a0use the variable <em>SlideSwitch<\/em> and the images of\u00a0the slider switch button.<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/01\/SlideSwitch.gif\" rel=\"attachment wp-att-1153\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1153 aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/01\/SlideSwitch.gif\" alt=\"SlideSwitch\" width=\"457\" height=\"262\" \/><\/a><\/p>\n<p>Lastly, the push button is handled in a somewhat different way. The goal is that while the button is pressed, the button should look like it is in the down position. Once the finger is removed from the button, the image should look like a button in the up position.<\/p>\n<p>This feature is implemented by processing the <em>TouchDown<\/em> event and then setting the image to the picture showing the button in the down position.<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/01\/TouchDown.gif\" rel=\"attachment wp-att-1154\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1154 aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/01\/TouchDown.gif\" alt=\"TouchDown\" width=\"453\" height=\"163\" \/><\/a>When the finger is removed, a <em>TouchUp<\/em> event occurs. In the <em>TouchUp<\/em>\u00a0event handler, the button&#8217;s image is reset to the image of the button in the up position.<\/p>\n<h2>Download Source<\/h2>\n<p>This .zip file contains the App Inventor .aia files and the graphic images. I drew these images using Affinity Designer software and I assert no rights to the images &#8211; feel free to use them anyway you wish.<\/p>\n<p>Right-click your mouse (on Windows) or Ctrl-click (on Mac OS X) and choose the save as option in your browser:\u00a0<a href=\"http:\/\/appinventor.pevest.com\/files\/PrettyButtons.zip\">PrettyButtons.zip<\/a><\/p>\n<p>Unzip the file and upload the .aia file to your App Inventor account.<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;<\/p>\n<p>I will be making some additional user interface components and will post here on this web site. Hopefully, with the simple example above, you can begin creating some components of your own!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In\u00a0the real world, &#8220;user interfaces&#8221; look like electric light switches, push buttons or control knobs, temperature dials on ovens, volume controls on radios and so forth. We can mimic these types of controls for our touch screen Android apps. We do not have to rely on the boring desktop-like clickable button or checkboxes in the &hellip; <a href=\"https:\/\/coldstreams.com\/appinventor\/2016\/01\/19\/making-pretty-app-inventor-user-interface-controls\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Making &#8220;pretty&#8221; App Inventor user interface controls<\/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":[2,5,10,12,14],"tags":[],"class_list":["post-1129","post","type-post","status-publish","format-standard","hentry","category-android-development","category-components","category-programming-method","category-stem-education","category-user-interfacedesign"],"_links":{"self":[{"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/posts\/1129","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=1129"}],"version-history":[{"count":0,"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/posts\/1129\/revisions"}],"wp:attachment":[{"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/media?parent=1129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/categories?post=1129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/tags?post=1129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}