I have created a custom layout which extends ViewGroup. Everything is working fine and I am getting the layout as expected.
I want to dynamically change an element in the layout. However this is not working as I am calling it in onCreate and till that time the entire layout is not actually (drawn) inflated onto the screen and hence do not have actual size.
Is there any event which can be used to find out when the inflation of the layout is done? I tried onFinishInflate but that would not work as Viewgroup has multiple views and this would be called multiple times.
I am thinking of creating an interface in the Custom layout class but not sure when to fire it?
If I understand your requirements correctly, an OnGlobalLayoutListener may give you what you need.
Usually when creating a custom layout extending
View
orViewGroup
, you have to overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
andprotected void onLayout(boolean changed, int left, int top, int right, int bottom)
. These are called during the process of inflation in order to obtain the size and location information related to the view. Also, subsequently, if you are extendingViewGroup
you are to callmeasure(int widthMeasureSpec, int heightMeasureSpec)
andlayout(int l, int t, int r, int b)
on every child view contained within. (measure() is called in onMeasure() and layout() is called in onLayout()).Anyway, in
onMeasure()
, you generally do something like this.In
onLayout()
you get the actual pixel coordinates of the View, so you can get the physical size like so: