This should be easy, but I can't figure it out. Always center a button...

So this is the code I have:

mainView = display.newGroup()   
local background = display.newRect(screenX, screenY, display.viewableContentWidth, display.viewableContentHeight)  
background:setFillColor(111, 111, 255)  
mainView:insert(background)  
  
myButton = widget.newButton{  
 id = "grabURL",  
 label = "Grab URL",  
 onEvent = grabURL,  
 emboss = true,  
 font=native.systemFont,  
 width=200  
}  
myButton.y = display.contentHeight \* 0.5  
myButton.x = (display.viewableContentWidth \* 0.5) - (myButton.width \* 0.5)  
mainView:insert(myButton.view)  

and then in my orientation change event I have:

myButton.x =(display.viewableContentWidth/2) - math.floor(myButton.contentWidth/2)  

I’ve tried tweaking everything in the world, and I can’t get it to align center when you rotate from portrait to landscape. I know I’m missing something, but I can’t figure out what.

Also, I have the button width set to 200, but when you go to landscape it upscales it to 300. Is there a way to prevent this upscaling on an object by object basis?

Thanks,
Scott.
[import]uid: 19193 topic_id: 13565 reply_id: 313565[/import]

is this what you are trying to achieve ?
[lua]local widget = require “widget”
mainView = display.newGroup()
local background = display.newRect(0, 0, display.viewableContentWidth, display.viewableContentHeight)
background:setFillColor(111, 111, 255)
mainView:insert(background)

local function grabURL()
print(“hello”)
end

local myButton = widget.newButton{
id = “grabURL”,
label = “Grab URL”,
onEvent = grabURL,
emboss = true,
font=native.systemFont,
width=200
}
myButton.y = display.contentHeight * 0.5
myButton.x = (display.viewableContentWidth * 0.5) - (myButton.width * 0.5)
mainView:insert(myButton.view)

local function orientationchange()
– this should work…but not workign as expected
–background.height = display.viewableContentHeight
–background.width = display.viewableContentWidth

background:removeSelf()
background = display.newRect(0, 0, display.viewableContentWidth, display.viewableContentHeight)
background:setFillColor(111, 111, 255)
mainView:insert(1,background)

myButton.y = display.contentHeight * 0.5
myButton.x = (display.viewableContentWidth * 0.5) - (myButton.width * 0.5)
end

Runtime:addEventListener(“orientation”, orientationchange)[/lua] [import]uid: 71210 topic_id: 13565 reply_id: 49849[/import]

Thanks! that did the trick. Don’t think I ever would have gotten that one. [import]uid: 19193 topic_id: 13565 reply_id: 50505[/import]