Gunny
Gunny

Reputation: 164

When are views drawn in android?

I want to get the size of a view that is in my activity but I am not able to get that information in any of the activity lifecycle callbacks (onCreate, onStart, onResume). I'm assuming this is because the views have not been drawn yet. At what point are views drawn and is there a callback I can put my code so I can get the size of the view?

findViewById(R.id.header).getHeight();

Upvotes: 4

Views: 2508

Answers (2)

Cheryl Simon
Cheryl Simon

Reputation: 46844

How are views drawn provides a good overview of the process of drawing views. Basically, there is a pass where the measure what everything wants to be, and then a second pass when things are layed out.

It sounds like for your problem though, you should be able to accomplish your goal with resorting to setting height values by hand. Have you played around with the stretchMode, gravity, layoutHeight, etc of your gridview? See GridView javadoc for some details of the param choices.

Upvotes: 3

Andy Zhang
Andy Zhang

Reputation: 8315

It should work in onCreate(), onStart(), and onResume() after you've instantiated your layouts. Try:

findViewById(R.id.header).getLayoutParams().height

Upvotes: 0

Related Questions