0

I am trying to draw a View in a ViewGroup without adding it to the child list. I am doing this because I want to display something like a ProgressBar in the exact center of layouts like a LinearLayout so I don't want the layout to handle the measuring and layouting.

I also don't want to complicate the view hierarchy by adding extra layouts just to achieve this effect so my solution was to extend the LinearLayout, create a ProgressBar and handle measuring, layouting and drawing for that view myself. My implementation seems to work ok from what I tested but I am wondering if there is anything I am not noticing or if there are any problems that can appear in the future.

From what I understand calling addView also sets the child view's parent and calls dispatchAttachedToWindow, these methods are package-private so I can't call them myself. Is there any side effect that can arise from calling measure, layout and draw on a view that has no parent and that was not "attached" to a window? Is there a safer way to achieve the same effect?

Thanks.

Valentin Iorgu
  • 521
  • 4
  • 5
  • What about a Animated Drawable drawing in your canvas ? refer http://stackoverflow.com/questions/4651618/is-it-possible-to-have-an-animated-drawable – Krish Jan 12 '15 at 13:00
  • That was my original implementation, but using a ProgressBar gives me more flexibility with a lot less code. If my current solution proves to be unsafe I'll probably revert to using a drawable. – Valentin Iorgu Jan 12 '15 at 13:11
  • So if in doubts, add the ProgressBar to your custom ViewGroup, just add one additional layer: root is FrameLayout wwhich has two children: LinearLayout and ProgressBar – pskink Jan 12 '15 at 13:27
  • @pskink, that is what I am trying to avoid, I don't want to complicate the view hierarchy by adding more layers just for this. – Valentin Iorgu Jan 12 '15 at 13:36
  • Did you see any performance issues that you want to avoid FrameLayout? – pskink Jan 12 '15 at 14:26
  • @pskink, there's performance on one side, the impact might not be high, but it is an extra layout that will have to be inflated and transversed and on the other side there's ease of use, it's simpler to call a method on your root layout than to wrap it in another layer and then handle the visibility of the various components, especially if you have to do this multiple times in multiple apps. – Valentin Iorgu Jan 12 '15 at 14:53

0 Answers0