I've had similar issues, never understood why it behaves so differently
even under the same OS... but this one always worked for me:
ViewTreeObserver vto = viewYouWantToWaitForItsLayout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener(){
@Override
public void onGlobalLayout(){
//here you should stop listening to it... so it will be
called only on the initial layout
}
}
On Friday, December 7, 2012 2:26:15 PM UTC-5, momo wrote:
>
> This question has been asked several times in various forms but I haven't
> found a definitive answer.
>
> I need to be able to get dimensions and positions of descendant Views
> after the initial layout of a container (a contentView). The dimensions
> required in this case are of a View(Group) that is not predictable - might
> have multiple lines of text, minimum height to accommodate decoration, etc.
> I don't want to do it every on every layout pass (onLayout). The
> measurement I need is a from a deeply nested child, so overriding the
> onLayout/onMeasure of each container in between seems a poor choice. I'm
> not going to do anything that'll cause a loop (trigger-event-during-event).
>
> Romain Guy hinted once that we could just .post a Runnable in the
> constructor of a View (which AFAIK would be in the UI thread). This seems
> to work on most devices (on 3 of the 4 I have personally), but fails on
> Nexus S. Looks like Nexus S doesn't have dimensions for everything until
> it's done one pass per child, and does not have the correct dimensions when
> the posted Runnable's run method is invoked. I tried counting layout passes
> (in onLayout) and comparing to getChildCount, which again works on 3 out of
> 4, but a different 3 (fails on Droid Razr, which seems to do just one pass
> - and get all measurements - regardless of the number of children). I tried
> various permutations of the above (posting normally; posting to a Handler;
> casting getContext() to an Activity and callingrunOnUiThread... same
> results for all).
>
> I ended up using a horrible shameful no-good hack, by checking the height
> of the target group against its parent's height - if it's different, it
> means it's been laid out. Obviously, not the best approach - but the only
> one that seems to work reliably between various devices and
> implementations. I know there's no built-in event or callback we can use,
> but is there a better way?
>
> TYIA
>
>
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en