Hey guys! Quick question. After a click a button, how to I change the colour and label from say a green button that says “Start” to a red on that says “End”?
can you provide the code for the button creation logic?
Sure!
local clicked = false -- Used to determine how many times the button has been clicked local widget = require("widget") function countTime( event ) -- Count Time Function Used By The Button if clicked ~= true then timeStart = os.time() print("Starting Time: " .. timeStart) clicked = true else print("Ending Time: " .. os.time()) timeFinal = os.difftime( os.time(), timeStart ) print("Final Time: " .. timeFinal) totalEarned = math.floor( ( (wage.text/60) /60) \* timeFinal ) display.newText( "You Earned: $" .. totalEarned, HW, 225, Arial ) local options = { to = { phoneNumber.text }, body = "I worked " .. timeFinal/MINUTE .. " minutes today which is $" .. totalEarned .. "! Thanks :)" } native.showPopup( "sms", options ) end end local button1 = widget.newButton( --Create The Button { label = "START", font = "BebasNeue.otf", fontSize = 50, labelYOffset = 2, labelColor = { default={0, 0, 0, 0.5 }, over={0, 0, 0, 0.5} }, shape = "roundedRect", width = 200, height = 80, cornerRadius = 10, fillColor = { default={146/255,205/255,0,0.5}, over={146/255,205/255,0,0.4} }, strokeColor = { default={1, 1, 1}, over={1, 1, 1, 0.75} }, strokeWidth = 8, onRelease = countTime } )
To my knowledge, you can’t dynamically change the label of a widget.newButton() after it’s been created, in normal code. However, the below forum thread talks a bit about how the characteristics of the buttons can be changed (color, to be specific) after creation.
The button:setLabel() method was added at some point in the last year or two (without much fanfare). Should get you what you want.
https://docs.coronalabs.com/daily/api/type/ButtonWidget/setLabel.html
[quote name=“schroederapps” post=“320562” timestamp=“1456689910”]The button:setLabel() method was added at some point in the last year or two https://docs.coronalabs.com/daily/api/type/ButtonWidget/setLabel.html[/quote] Shows how often I’ve been reading the docs lately. Thanks Jay!
can you provide the code for the button creation logic?
Sure!
local clicked = false -- Used to determine how many times the button has been clicked local widget = require("widget") function countTime( event ) -- Count Time Function Used By The Button if clicked ~= true then timeStart = os.time() print("Starting Time: " .. timeStart) clicked = true else print("Ending Time: " .. os.time()) timeFinal = os.difftime( os.time(), timeStart ) print("Final Time: " .. timeFinal) totalEarned = math.floor( ( (wage.text/60) /60) \* timeFinal ) display.newText( "You Earned: $" .. totalEarned, HW, 225, Arial ) local options = { to = { phoneNumber.text }, body = "I worked " .. timeFinal/MINUTE .. " minutes today which is $" .. totalEarned .. "! Thanks :)" } native.showPopup( "sms", options ) end end local button1 = widget.newButton( --Create The Button { label = "START", font = "BebasNeue.otf", fontSize = 50, labelYOffset = 2, labelColor = { default={0, 0, 0, 0.5 }, over={0, 0, 0, 0.5} }, shape = "roundedRect", width = 200, height = 80, cornerRadius = 10, fillColor = { default={146/255,205/255,0,0.5}, over={146/255,205/255,0,0.4} }, strokeColor = { default={1, 1, 1}, over={1, 1, 1, 0.75} }, strokeWidth = 8, onRelease = countTime } )
To my knowledge, you can’t dynamically change the label of a widget.newButton() after it’s been created, in normal code. However, the below forum thread talks a bit about how the characteristics of the buttons can be changed (color, to be specific) after creation.
The button:setLabel() method was added at some point in the last year or two (without much fanfare). Should get you what you want.
https://docs.coronalabs.com/daily/api/type/ButtonWidget/setLabel.html
[quote name=“schroederapps” post=“320562” timestamp=“1456689910”]The button:setLabel() method was added at some point in the last year or two https://docs.coronalabs.com/daily/api/type/ButtonWidget/setLabel.html[/quote] Shows how often I’ve been reading the docs lately. Thanks Jay!