{"id":1187,"date":"2016-03-17T20:53:03","date_gmt":"2016-03-18T03:53:03","guid":{"rendered":"http:\/\/appinventor.pevest.com\/?p=1187"},"modified":"2016-03-17T20:53:03","modified_gmt":"2016-03-18T03:53:03","slug":"can-you-gray-out-a-button-until-data-entry-is-complete","status":"publish","type":"post","link":"https:\/\/coldstreams.com\/appinventor\/2016\/03\/17\/can-you-gray-out-a-button-until-data-entry-is-complete\/","title":{"rendered":"Can you &#8220;gray out&#8221; a button until data entry is complete?"},"content":{"rendered":"<p>A reader asked\u00a0 if it might be possible to &#8220;gray out&#8221; a button so that pressing it has no action, until appropriate data has been entered?<\/p>\n<p>The answer is &#8220;Yes, we can do this.&#8221; After some thought, I came up with the following simple solution.<\/p>\n<p><strong>Update 1<\/strong>: <em>Check the comments to this post for a reader&#8217;s great solution for doing this for Location services dependent function.<\/em><\/p>\n<p><strong>Update 2:<\/strong> Also, you can set the button component&#8217;s <em>Enabled<\/em> property to <em>false<\/em>, so that the button will not function. Then set <em>Enabled<\/em> to <em>true<\/em> once the data entry meets your app&#8217;s requirements.<\/p>\n<h2>User Interface<\/h2>\n<p>What we want to do is have the button look like it is &#8220;grayed out&#8221; and unusable until after some data is entered into the field. In the text box, I have set the\u00a0 &#8220;hint&#8221; value to &#8220;Button available when data entered&#8221;:<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/03\/Screenshot_20160317-202312.png\" rel=\"attachment wp-att-1191\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1191 aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/03\/Screenshot_20160317-202312.png\" alt=\"Screenshot_20160317-202312\" width=\"279\" height=\"496\" \/><\/a>After the user has entered some data, the button becomes &#8220;active&#8221; as shown here:<\/p>\n<h2><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/03\/Screenshot_20160317-202334.png\" rel=\"attachment wp-att-1192\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1192 aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/03\/Screenshot_20160317-202334.png\" alt=\"Screenshot_20160317-202334\" width=\"277\" height=\"492\" \/><\/a><\/h2>\n<h2><\/h2>\n<p><!--more--><\/p>\n<h2>Designer View<\/h2>\n<p>Set up this app by dragging a horizontal layout to the top, and then adding a label (with its text changed to &#8220;Data entry&#8221; and a text box data entry field to its right.<\/p>\n<p>Add a button. On the button&#8217;s properties, set the text color to white (instead of the usual black). This makes the button appear to be &#8220;grayed out&#8221; (see screen shot).<\/p>\n<p>Drag a <em>Clock<\/em> component from the Sensors category, to the designer window. On the <em>Clock<\/em>&#8216;s properties, set the timer interval to a small value such as 250 milliseconds. The <em>Clock<\/em>&#8216;s event handler will continuously check the data entry field for proper data.<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/03\/Voila_Capture-2016-03-17_08-16-43_PM.gif\" rel=\"attachment wp-att-1188\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1188 aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/03\/Voila_Capture-2016-03-17_08-16-43_PM.gif\" alt=\"Voila_Capture 2016-03-17_08-16-43_PM\" width=\"287\" height=\"484\" \/><\/a>The components list &#8211; I renamed the text box to <em>txtDataEntry<\/em> and the button control to <em>btnEnterData<\/em>.<\/p>\n<h2>Blocks View<\/h2>\n<p>The implementation is easy. First, set up a global variable, which I have named <em>DataEntered<\/em> and set it to <em>false<\/em>. Once sufficient or correct data has been entered, the code changes <em>DataEntered<\/em> to <em>true<\/em>.<\/p>\n<p>The<em> btnEnterData.Click<\/em> event handler consists of an <em>if-then<\/em> block. It checks to see if <em>DataEntered<\/em> is true; if it is, then it executes the blocks of code for processing the button (in this example, I have left this blank &#8211; insert your own code blocks to do something!)<\/p>\n<p>The guts of the program are handled in the <em>Clock&#8217;s Timer<\/em> event handler. Every 250 milliseconds, we do a simple test to see if data has been entered and if at least 4 characters of data have been type in the text box. If so, then we set <em>DataEntered<\/em> to true and we turn the text color on the button to black so that the button appears to now be active. You can add more complex tests here &#8211; has a date been entered in the proper format? Has a password been entered with at least one upper case letter and one number? You can implement whatever rules you want to have here.<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/03\/Voila_Capture-2016-03-17_08-15-46_PM.gif\" rel=\"attachment wp-att-1190\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1190\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/03\/Voila_Capture-2016-03-17_08-15-46_PM.gif\" alt=\"Voila_Capture 2016-03-17_08-15-46_PM\" width=\"448\" height=\"332\" \/><\/a><\/p>\n<p>The text box control has two events &#8211; <em>gotfocus<\/em> and <em>lostfocus<\/em>. Initially I tried to use those to implement this feature, but this approach does not work. We want the button to change to &#8220;active&#8221; as soon as the proper data has been entered. But the <em>gotfocus<\/em> event occurs only when you first tap the text box entry field and <em>lostfocus<\/em> occurs only when you have moved elsewhere in the user interface.<\/p>\n<h2>Tricks<\/h2>\n<p>What if you have more than one control you wish to have &#8220;grayed out&#8221;? Simple implement the same code as above, but add a 2nd (or 3rd) Clock component to your app. Your app can have multiple clocks. Then implement the appropriate code for the additional clock event handlers and button handlers.<\/p>\n<p>You could also implement this feature for say, a series of check boxes. For example, suppose you have 4 check boxes on screen and the user may select 1 to 4 of them, but the app should not continue until at least one box has been selected. Your &#8220;Continue&#8221; button would be grayed out until at least one check box has been selected. Once a check box value has been selected you can use code similar to the above to activate the button.<\/p>\n<h2>Download Source Code<\/h2>\n<p><a href=\"http:\/\/appinventor.pevest.com\/files\/GrayBtnDataVerify.aia\">GrayBtnDataVerify.aia<\/a><\/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 <em>and printed book<\/em>)<\/strong><br \/>\nStep-by-step guide to graphics, animation and charts<\/li>\n<\/ul>\n<p>Thank you for visiting! &#8212; Ed<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A reader asked\u00a0 if it might be possible to &#8220;gray out&#8221; a button so that pressing it has no action, until appropriate data has been entered? The answer is &#8220;Yes, we can do this.&#8221; After some thought, I came up with the following simple solution. Update 1: Check the comments to this post for a &hellip; <a href=\"https:\/\/coldstreams.com\/appinventor\/2016\/03\/17\/can-you-gray-out-a-button-until-data-entry-is-complete\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Can you &#8220;gray out&#8221; a button until data entry is complete?<\/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,13],"tags":[],"class_list":["post-1187","post","type-post","status-publish","format-standard","hentry","category-components","category-programming-method","category-tips"],"_links":{"self":[{"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/posts\/1187","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=1187"}],"version-history":[{"count":0,"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/posts\/1187\/revisions"}],"wp:attachment":[{"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/media?parent=1187"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/categories?post=1187"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/tags?post=1187"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}