Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

noob: how to center a view

I have created a full screen window, created a view with left and top set to 0. Then I created another view and want to center that within first view, both horizontally and vertically. My attempts to get the left, top, width and height values from either the window of the first view have failed, perhaps due to my not know how to retrieve this information properly. I presume I would have problems retrieving the other dimension/size properties. Can anyone help? Thanks!

— asked June 19th 2010 by Bill Turner
  • android
  • center
  • mobile
  • phone
  • view

1 Answer

  • Accepted Answer

    Set the width & height of the 2nd view, don't set the left/right/bottom/top and add it to the first view, it should centre in the containing view.

    — answered June 20th 2010 by David Ashwood
    • Thanks, David! Is there any way to find out how much real estate is available to the inner view? What I would like to achieve is a relatively sized inner view. Ultimately what I am trying to achieve is that my inner view will be, say 80% the width and height of the outer view, and centered both ways within the outer view. I haven't tested your response yet, but presume the latter half of my concerns have been answered.

      — commented June 20th 2010 by Bill Turner
    • Currently not easily or without doing the math yourself. I believe there's been some commits recently that calc relevant related properties but I don't know if they've made it to Android or just iPhone.

      — commented June 20th 2010 by David Ashwood
    • okay, i can do math, but the problem is, how do i know the size of the screen, window, or outer view? any one of which would be helpful. i have used debug to show myview.height, for example, and the value displayed is null. i cannot do math on null. same goes for the left and top properties. i didn't bother checking other properties, for i seemed likely the result would be the same. what am i missing? do i have to keep track of all the possible android models, determine on which model my app is running, and then do the math? or, am i just not referencing/retrieving the properties correctly? if so, what do i need to do to get the values i need?

      — commented June 22nd 2010 by Bill Turner
    • Until the property cascade/values are fixed - you'll need to wrap the object in your own code to define the widths.

      1) Create Parent Window - fullscreen - so the height & width are from DeviceCaps.width & height - win.cHeight = xx, Win.cWidth = yy
      2) Create a view that's left:5px, top:5px; view.cHeight = (win.cHeight-5), view.cWidth = (win.cWidth-5), etc.
      You can wrap it fairly easily with some helper functions. It's slightly painful - but it'll work with any screen-size as the DeviceCaps should have the right values.

      We do something similar with % measurements currently. You end up with code that's a little more verbose but it's pretty reliable.

      — commented June 24th 2010 by David Ashwood
    • Good! This is one problem that's (thankfully) easy to solve: views are, so to speak, self-centered. ;-)

      — commented December 15th 2011 by Mark Pemburn
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.