corona coverting radio button to numeric textbox (lua) - lua

I am trying to create a mobile application about healthcare. I have to create a survey like application to screen the patients. Right now i have completed a part of the survey using radio button, the other part also look something like this(see picture below) but i want the radio button change to numeric textbox. My application look something like this:
Right now I want to convert the radio button to a numeric textbox.
The question is: how can i make minimum code changes and replace all the radio button to numeric textbox?
Here is my code for the radio buttons:
local radioGroup = display.newGroup()
if(ebasRating_Arr[i] == 0) then
radioButton_0 = widget.newSwitch {
left = 565,
style = "radio",
initialSwitchState = true,
id = tostring(i),
width = 60,
height = 60,
onPress = setEBASRating0
}
radioGroup:insert( radioButton_0 )
radioButton_1 = widget.newSwitch {
style = "radio",
id = tostring(i),
initialSwitchState = false,
width = 60,
height = 60,
onPress = setEBASRating1
}
radioGroup:insert( radioButton_1 )
elseif (ebasRating_Arr[i] == 1) then
radioButton_0 = widget.newSwitch {
left = 565,
style = "radio",
initialSwitchState = false,
id = tostring(i),
width = 60,
height = 60,
onPress = setEBASRating0
}
radioGroup:insert( radioButton_0 )
radioButton_1 = widget.newSwitch {
style = "radio",
id = tostring(i),
initialSwitchState = true,
width = 60,
height = 60,
onPress = setEBASRating1
}
radioGroup:insert( radioButton_1 )
else
radioButton_0 = widget.newSwitch {
left = 565,
style = "radio",
initialSwitchState = false,
id = tostring(i),
width = 60,
height = 60,
onPress = setEBASRating0
}
radioGroup:insert( radioButton_0 )
radioButton_1 = widget.newSwitch {
style = "radio",
id = tostring(i),
initialSwitchState = false,
width = 60,
height = 60,
onPress = setEBASRating1
}
radioGroup:insert( radioButton_1 )
end
radioButton_0.y = 150 + (i * 450)
radioButton_1.x = 18 + radioButton_0.x+radioButton_0.width
radioButton_1.y = 150 + (i * 450)
scrollView:insert( radioGroup )
end

Related

Kivy: ScrollView squishes together the layout

I am new to Kivy and this is my first time trying to create a scrollView.
I am trying to do a 'Splitwise' ripoff where you can create expenses and let the app figure out who should pay who etc.
I am trying to create a method that opens a popup with all members and their balances as well as a button with the text 'Settle up'. Underneath the balances should be a list of all the registered debts, and you should be able to scroll down to see them all. The popup should also contain a 'Cancel'-button.
My problem is that my code doesn't enable scrolling, and also squishes together the content in the pop up. See picture.
All help highly appreciated!
See (messy) code below:
sbl_ret = GridLayout(cols = 1)
sbl_balances = GridLayout(cols = 3)
sbl_balances.add_widget(Label())
sbl_balances.add_widget(Label(text = 'Balances', font_size = '20sp', size_hint = (1,.2), color = backgroundColor, bold = True))
sbl_balances.add_widget(Label())
for member in self.members:
sbl_balances.add_widget(Label(text = member.getName(), font_size = 40, size_hint_y = None, height = 60))
sbl_balances.add_widget(Label(text = str(round(member.getBalance())) + ' kr', font_size = 40, size_hint_y = None, height = 60))
sbl2 = BoxLayout(orientation = 'vertical')
sbl2.add_widget(Label(size_hint = (1,.2)))
btn = Button(text = 'Settle up', background_color = buttonColor, bold = True, size_hint_y = None, height = 60)
btn.bind(on_press = lambda member: self.settleUp())
sbl2.add_widget(btn)
sbl2.add_widget(Label(size_hint = (1,.2)))
sbl_balances.add_widget(sbl2)
sbl_balances.add_widget(Label(size_hint = (.2,1)))
sbl_balances.add_widget(Label(text = 'Debts', font_size = '20sp', color = backgroundColor, bold = True))
sbl_balances.add_widget(Label(size_hint = (.2,1)))
for member in self.members:
for debt in member.getDebtList():
sbl_balances.add_widget(Label(size_hint = (.2,1)))
sbl_balances.add_widget(Label(size_hint_y = None, height = 60, text = debt.getToWhom().getName() + ' lent ' + str(debt.getAmount()) + ' SEK to ' + debt.getFromWhom().getName() + ' for ' + debt.getDescription(), font_size = 40))
sbl_balances.add_widget(Label(size_hint = (.2,1)))
sbl_balances.bind(minimum_height=sbl_balances.setter('height'))
sbl3 = BoxLayout(size_hint = (1,.15))
sbl3.add_widget(Label(size_hint = (.2,1)))
b = Button(text = 'Cancel', background_color = entryColor, size_hint_y = None, height = 150)
b.bind(on_press = lambda x: self.balanceDialog.dismiss())
sbl3.add_widget(b)
sbl3.add_widget(Label(size_hint = (.2,1)))
sv = ScrollView(do_scroll_x = False)
sv.add_widget(sbl_balances)
sbl_ret.add_widget(sv)
sbl_ret.add_widget(sbl3)
self.balanceDialog = Popup(title = 'Group view', content = sbl_ret, title_align = 'center',
title_color = backgroundColor, background_color = [0,0,0,.7], separator_height = 0, title_size = '28sp')
self.balanceDialog.open()
'''
Try adding size_hint_y=None to your code creating the GridLayout:
sbl_balances = GridLayout(cols = 3, size_hint_y=None)
See the documentation.

ScrollView doesn't work - corona, lua

I'm trying to add object to the ScrollView and this is what happen:
ERROR:Table expected. if this is a function call, you might have used '.' instead of ':
What is that?
code:
This code is works:
scrollView = widget.newScrollView
{ -- ScrollView settings
width = _W,
height = 1760,
horizontalScrollDisabled = true,
hideBackground = true,
friction = 0.988,
x = display.contentCenterX,
y = _H*0.55,
isBounceEnabled = false,
bottomPadding = 260,
hideScrollBar = true
}
for i = 1, #allballs do
Frames[i] = display.newImage( "Frame.png" )
Frames[i].height = 0.12*_H
Frames[i].width = 0.38*_W
Frames[i].ballImage = allballs[i].Image
Frames[i].Purchased = allballs[i].Purchased
Frames[i].Price = allballs[i].Price
Frames[i].Place = i
if (i%2==0) then
Frames[i].y=(i-1)*140
Frames[i].x = 0.27*_W
PositionY = (i-1)*140
PositionX = 0.27*_W
else
Frames[i].y=i*140
Frames[i].x = 0.73*_W
PositionY = i*140
PositionX = 0.73*_W
end
if (allballs[i].Purchased) then
balls[i] = display.newImage( allballs[i].Image )
balls[i].height = 0.15*_W
balls[i].width = 0.15*_W
balls[i].x = PositionX
balls[i].y = PositionY
else
local txtoptions =
{
text = allballs[i].Price,
x = PositionX+0.055*_W,
y = PositionY,
width = 0.23*_W,
font = native.systemFontBold,
fontSize = _W*0.1,
align = "left"
}
balls[i] = display.newText( txtoptions )
end
Frames[i]:setFillColor( 1, 1, 0.4 )
Frames[i]:addEventListener( "tap", sellectball )
scrollView:insert( Frames[i] )
scrollView:insert( balls[i] )
end
Everything works perfectly. But when I add this (between the ***):
for i = 1, #allballs do
Frames[i] = display.newImage( "Frame.png" )
Frames[i].height = 0.12*_H
Frames[i].width = 0.38*_W
Frames[i].ballImage = allballs[i].Image
Frames[i].Purchased = allballs[i].Purchased
Frames[i].Price = allballs[i].Price
Frames[i].Place = i
if (i%2==0) then
Frames[i].y=(i-1)*140
Frames[i].x = 0.27*_W
PositionY = (i-1)*140
PositionX = 0.27*_W
else
Frames[i].y=i*140
Frames[i].x = 0.73*_W
PositionY = i*140
PositionX = 0.73*_W
end
if (allballs[i].Purchased) then
balls[i] = display.newImage( allballs[i].Image )
balls[i].height = 0.15*_W
balls[i].width = 0.15*_W
balls[i].x = PositionX
balls[i].y = PositionY
else
local txtoptions =
{
text = allballs[i].Price,
x = PositionX+0.055*_W,
y = PositionY,
width = 0.23*_W,
font = native.systemFontBold,
fontSize = _W*0.1,
align = "left"
}
balls[i] = display.newText( txtoptions )
***** coin = display.newImage( "coin.png", PositionX-0.07*_W, PositionY)
coin.width = _W*0.05
coin.height = _H*0.05 *******
end
Frames[i]:setFillColor( 1, 1, 0.4 )
Frames[i]:addEventListener( "tap", sellectball )
scrollView:insert( Frames[i] )
scrollView:insert( balls[i] )
******* scrollView:insert( coin ) ********
end
It dosent work...
youre probably calling the method like object.method() instead of object:method()
difference is that : syntax uses automatically this as reference to caller object, with . syntax you have to type it explicitly as first parameter
The error always occurs for the reason like Andoloon pointed out.
but it doesn't seem to be in your ******* code. The problem for the ***** section might be that your 'coin' var is not a list, but it is defined and inserted in the loop. Try move it out of the loop and also remember to use local before the declearation.
I'm guessing that coin.png doesn't exist, or may not be an exact file name match (if you're doing this on a device). Look for a warning in your console log about missing images.

Corona app's purple background in xCodeSimulator

I have a corona app, with two storyboard scenes (scene1.lua, addDesire.lua).
I show addDesire.lua as an overlay:
function onAddPurchase( event )
if event.phase == "ended" then
local options = {
effect = "fromBottom",
time = 500,
isModal = true,
}
storyboard.showOverlay( "addDesire", options )
end
end
In Corona Simulator everything works, but in xCode simulator a pink background appears in several cases.
1) When addDesire.lua appears after onAddPurchase it looks like:
When it should look like:
2) When I close addDesire.lua (tapping on Cancel button) this appears:
There is something even stranger going on under the hood:
addDesire.lua has 2 textFields and 1 textBox which is created in function scene:createScene( event ). If I comment out the code that creates these objects everything works perfectly.
function scene:createScene( event )
local group = self.view
local centerX = display.contentCenterX
local centerY = display.contentCenterY
local _W = display.contentWidth
local _H = display.contentHeight
local bg = display.newImageRect( "assets/dollar.png", 360, 570 )
bg:toBack()
bg.x, bg.y = _W/2, _H/2
bg:addEventListener( "tap", function() native.setKeyboardFocus(nil); end)
group:insert(bg)
-- Rounded Rect Alpha
roundedRect = display.newRoundedRect( 5, 5, _W*0.9, _H*0.8, 10 )
roundedRect.x, roundedRect.y = centerX, centerY
roundedRect:setFillColor( 0/255, 0/255, 0/255, 170/255 )
group:insert(roundedRect)
-- Label Title
titleLabel = display.newText( "Purchase", 0, 0, "AmericanTypewriter-Bold", 20 )
titleLabel.x, titleLabel.y = centerX, _H*0.15
group:insert(titleLabel)
-- Label Fam
nameLabel = display.newText( "Name", 0, 0, "AmericanTypewriter", 18 )
nameLabel.x, nameLabel.y = centerX, _H*0.20
group:insert(nameLabel)
-- Edit Fam
nameText = native.newTextField(_W/2, _H*0.26, 240, 30)
nameText.font = native.newFont(native.systemFont, 18)
nameText:addEventListener( "userInput", inputListener )
group:insert(nameText)
-- Label Name
descriptionLabel = display.newText( "Description", 0, 0, "AmericanTypewriter", 16 )
descriptionLabel.x, descriptionLabel.y = centerX, _H*0.31
group:insert(descriptionLabel)
-- Edit Name
descriptionText = native.newTextBox(_W/2, _H*0.44, 240, 100)
descriptionText.font = native.newFont(native.systemFont, 14)
descriptionText.isEditable = true
descriptionText:addEventListener( "userInput", inputListener )
group:insert(descriptionText)
-- Label Deposit
costLabel = display.newText( "Cost", 0, 0, "AmericanTypewriter", 16 )
costLabel.x, costLabel.y = centerX, _H*0.57
group:insert(costLabel)
-- Edit Deposit
costText = native.newTextField(_W/2, _H*0.64, 240, 30)
costText.font = native.newFont(native.systemFont, 18)
costText.inputType = "number"
costText.align = "center"
costText:addEventListener( "userInput", inputListener )
group:insert(costText)
-- Button Save & Start game
btnSave = widget.newButton {
width = _W*0.4,
height = 50,
--defaultFile = "buttonDefault.png",
--overFile = "buttonOver.png",
label = "Buy",
onEvent = onSaveData
}
btnSave.x, btnSave.y = _W/2, costLabel.y+150
group:insert(btnSave)
-- Button Cancel
btnCancel = widget.newButton {
width = _W*0.8,
height = 50,
--defaultFile = "buttonDefault.png",
--overFile = "buttonOver.png",
label = "Cancel",
onEvent = onExit,
}
btnCancel.x, btnCancel.y = _W/2, btnSave.y+50
group:insert(btnCancel)
end
What is going on?
The answer is here guys: native.textField

Connecting my tab buttons to pages

I'm creating a basic quiz game in corona for a class and am having troubles getting my tab bar buttons to connect to the other pages that have been created for them. I was just wondering if anyone out there could help me out.
main.lua
local background = display.newImage ("basketball_court.jpg")
print("PE Sports Quiz")
display.setStatusBar( display.HiddenStatusBar )
local textObj = display.newText("PE Sports Quiz",
100, 50, m11, 24)
textObj:setTextColor(250,250,250)
local widget = require ( "widget" )
local storyboard = require("storyboard")
local scene = storyboard.newScene()
local localGroup = display.newGroup()
local tabButtons =
{
{
width = 32, height = 32,
defaultFile = "tab.png",
overFile = "tab2.png",
label = "Play",
onPress = function() storyboard.gotoScene( "basketball1" ); end,
selected = true
},
{
width = 32, height = 32,
defaultFile = "tab.png",
overFile = "tab2.png",
label = "Credits",
onPress = function() storyboard.gotoScene( "credits" ); end,
}
}
local demoTabs = widget.newTabBar
{
top = display.contentHeight - 50,
width = display.contentWidth,
backgroundFile = "back.png",
tabSelectedLeftFile = "tab.png",
tabSelectedMiddleFile = "tab.png",
tabSelectedRightFile = "tab.png",
tabSelectedFrameWidth = 20,
tabSelectedFrameHeight = 52,
buttons = tabButtons
}
local tabBar = widget.newTabBar
{
top = display.contentHeight - 50,
width = display.contentWidth,
buttons = tabButtons
}
The only thing I see is that you should rename your credits.lua to gamecredits.lua and change the gotoScene() call for it. There is an internal module called "credits" that can cause issues.

In onButtonEvent function t.value not updating

I am designing an Odd One Out round for a game that I am developing, every time a button is pressed, the onButtonEvent function is supposed to check if the word selected is the odd one out and then refreshes the buttons with different words.
The words are updating and the value assigned to each button is updating, I have been printing it out to the console. I think the problem is when I assign right[num] = t.value the only values that go into the table are the first 3 values that were originally assigned to the buttons. Why is this not being updated?
local function onButtonEvent( event )
local t = event.target
--local phase = event.phase
if event.phase == "release" then
--num = num + 1
displayBtns()
right[num] = t.value
print(right[1])
print(right[2])
print(right[3])
print(right[4])
-- --s3 = table.concat(right)
--print(s3)
if right[num] == answers[num].right then
print("correct!")
elseif right[num] == answers[num].wrong then
-- t.alpha = 0
-- select.alpha = 1
print("incorrect!")
elseif right[num] == answers[num].wrong2 then
else
print("incorrect!")
end
num = num + 1
num2 = num2 + 1
s3 = ""
-- display.remove( btn3 )
-- btn3 = nil
-- display.remove( btn2 )
-- btn2 = nil
-- display.remove( btn1 )
-- btn1 = nil
end
--num = math.random(1,#t)
s3 = ""
end
function displayBtns()
btn3 = widget.newButton{
default = "images/Round4_blue_button.png",
label = answers[num].right,
fontSize = 22,
labelColor = { default={ 255, 250, 250}, over={ 0, 0, 0,} },
onEvent = onButtonEvent
}
btn3.value = answers[num].right
print(btn3.value)
btn2 = widget.newButton{
default = "images/Round4_blue_button.png",
label = answers[num].wrong2,
fontSize = 22,
labelColor = { default={ 255, 250, 250}, over={ 0, 0, 0,} },
onEvent = onButtonEvent
}
btn2.value = answers[num].wrong2
print(btn2.value)
btn1 = widget.newButton{
default = "images/Round4_blue_button.png",
label = answers[num].wrong,
fontSize = 22,
labelColor = { default={ 255, 250, 250}, over={ 0, 0, 0,} },
onEvent = onButtonEvent
}
btn1.value = answers[num].wrong
print(btn1.value)
print("----------")
--btn3.label = answers[num].right
-- p1Button = display.newImage("images/SinglePlayer_button.png", 90, 140)
-- p2Button = display.newImage("images/2Player_button.png", 90, 220)
-- p1Button.touch = onSceneTouch
-- p1Button:addEventListener("touch", p1Button)
-- p2Button.touch = onSceneTouch1
-- p2Button:addEventListener("touch", p2Button)
btn1.x = 90; btn1.y = 245
btn2.x = 240; btn2.y = 245
btn3.x = 390; btn3.y = 245
-- p2Button.x = 240; p2Button.y = 260
end
displayBtns()
The problem is probably here:
elseif right[num] == answers[num].wrong2 then
else
print("incorrect!")
end
You have nothing set in the elseif statement.

Resources