I'm perplexed why the dimension accessors on Views generally return
0. I have specified fill_parent as their layout_width and
layout_height, yet calls to getWidth(), getRight(), etc. all return 0.
Does this make any sense? How do I find out how big a view is?
On Tue, Dec 9, 2008 at 9:24 AM, Keith Wiley <kbwi...@gmail.com> wrote:
> I'm perplexed why the dimension accessors on Views generally return
> 0. I have specified fill_parent as their layout_width and
> layout_height, yet calls to getWidth(), getRight(), etc. all return 0.
> Does this make any sense? How do I find out how big a view is?
Note: please don't send private questions to me, as I don't have time to
provide private support. All such questions should be posted on public
forums, where I and others can see and answer them.
I'm sorry, I really don't know what you're trying to point me to. I
have thoroughly read the docs for the class android.view.View. Is
that what you're referring to when you say "View docs on layout" or do
you mean a different document? I am afraid that while that
documentation is clearly thorough, it is not necessarily unambiguous,
as I still don't understand how to ask a view what its width is.
Sorry for the trouble.
Am I supposed to be reading some other documentation?
Thanks.
On Dec 9, 9:25 am, "Dianne Hackborn" <hack...@android.com> wrote:
> On Tue, Dec 9, 2008 at 9:24 AM, Keith Wiley <kbwi...@gmail.com> wrote:
> > I'm perplexed why the dimension accessors on Views generally return
> > 0. I have specified fill_parent as their layout_width and
> > layout_height, yet calls to getWidth(), getRight(), etc. all return 0.
> > Does this make any sense? How do I find out how big a view is?
> Note: please don't send private questions to me, as I don't have time to
> provide private support. All such questions should be posted on public
> forums, where I and others can see and answer them.
Yes, it's a little tricky. And I certainly don't consider the docs to
be thorough.... more of a work in progress.
Anyhow, the deal is that layout of the contents of a window happens
*after* all the elements are constructed and added to their parent
views. It has to be this way, because until you know what components
a View contains, and what they contain, and so on, there's no sensible
way you can lay it out.
Bottom line, if you call getWidth() etc. in a constructor, it will
return zero. The procedure is to create all your view elements in the
constructor, then wait for your View's onSizeChanged() method to be
called -- that's when you first find out your real size, so that's
when you set up the sizes of your GUI elements.
Be aware too that onSizeChanged() is sometimes called with parameters
of zero -- check for this case, and return immediately (so you don't
get a divide by zero when calculating your layout, etc.). Some time
later it will be called with the real values.
> Yes, it's a little tricky. And I certainly don't consider the docs to
> be thorough.... more of a work in progress.
> Anyhow, the deal is that layout of the contents of a window happens
> *after* all the elements are constructed and added to their parent
> views. It has to be this way, because until you know what components
> a View contains, and what they contain, and so on, there's no sensible
> way you can lay it out.
> Bottom line, if you call getWidth() etc. in a constructor, it will
> return zero. The procedure is to create all your view elements in the
> constructor, then wait for your View's onSizeChanged() method to be
> called -- that's when you first find out your real size, so that's
> when you set up the sizes of your GUI elements.
> Be aware too that onSizeChanged() is sometimes called with parameters
> of zero -- check for this case, and return immediately (so you don't
> get a divide by zero when calculating your layout, etc.). Some time
> later it will be called with the real values.