11

I need access to already evaluated width and height of all component in Fragment view. So I need some notification which tell me, that view of fragment layouting phase is already done.

In activity I can use onWindowFocusChanged(boolean hasFocus) life cycle method, but fragment doesn't have this method.

Only way that i found is use getView().addOnLayoutChangeListener(). But it call multiple times and only last call is usefull for me.

Exist any better way how to call some after layout is done in fragment's view?

ATom
  • 15,960
  • 6
  • 46
  • 50
  • Related question http://stackoverflow.com/questions/14397959/capture-layout-resize-before-api-11 – Gelldur Mar 13 '15 at 12:21

1 Answers1

1

You can actually trigger a layout pass manually - you just need to call View.measure(int,int) with the appropriate MeasureSpecs. For example if the Fragment is to be attached to a parent view parentView, and you want it to have the same size, you'd do this:

View fragmentView = fragment.getView();
fragmentView.measure(MeasureSpec.makeMeasureSpec(parentView.getMeasuredWidth(),
                                                 MeasureSpec.EXACTLY),
                     MeasureSpec.makeMeasureSpec(parentView.getMeasuredHeight(),
                                                 MeasureSpec.EXACTLY));

More about how the measure/layout passes work: http://developer.android.com/reference/android/view/View.html#Layout

Gelldur
  • 11,187
  • 7
  • 57
  • 68
Marcus Forsell Stahre
  • 3,766
  • 1
  • 17
  • 14
  • But measure give only recommended size, real size is know after layout pass. And I don't need it run it manually, because Android run it again itself (I think). – ATom May 02 '12 at 17:19
  • It's actually not just a recommended size. The measure pass is used to tell the view that "this is what you get, use it as you wish". For example in a ListView, the constraint for width is EXACTLY, and for height it is UNSPECIFIED. You are correct that measured size isn't always the same as layouted size, but it almost always is. I'm not really sure what you're trying to do? – Marcus Forsell Stahre May 03 '12 at 08:16
  • I only need to be notified after fragment and its content already finished measure and layout pass and all dimensions are final. – ATom May 08 '12 at 09:20
  • Because onResume() is called before layout is measured. – ATom May 23 '12 at 13:34
  • 1
    onStart() is called before onResume(). –  Jan 22 '13 at 03:24