1

I have a custom ViewGroup that contains an ImageView and a few other TextViews that display white text on the image. I would like to draw ONE dark rectangle under all the textviews to improve the contrast.

To accomplish this, in the custom ViewGroup's onLayout, I compute the smallest rectangle that contain all of the TextViews. Now, I have no idea how to draw the rectangle on top of the first child (the image), but below the other children, the TextViews. So I would like to draw the children in the order:

  1. ImageView (first child)
  2. Computed dark Rect
  3. TextViews (all other children)

I tried overriding dispatchDraw() and then after the call to super.dispatchDraw(canvas), I drew the rectangle. But that obviously put the rectangle over the TextViews.

I would appreciate any help.

Thanks!

Stralo
  • 474
  • 4
  • 16
  • add additional `View` that draws its background and "place" in between first and the others (that is index == 1) – pskink Oct 01 '15 at 07:19
  • If you are using TextView and want to dark rect behind text of TextView then set background of TextView it will resolve your issue. – VikasGoyal Oct 01 '15 at 07:24
  • @Avi, I would like to use only one background for all the different textviews because I'm going to be applying one gradient for the textviews. – Stralo Oct 01 '15 at 07:26
  • @pskink - Sounds like it might work. Thanks for the idea. I'm guessing you mean that in my onLayout, I create a view from the computed Rect and then use t addChild(view, index). Is that correct? I'm going to try that. Any tips on how to create a View from a Rect object? – Stralo Oct 01 '15 at 07:28
  • 1
    in `onLayout` you just need to call `child.layout(l, t, r, b)` like any other child view – pskink Oct 01 '15 at 07:30
  • Then what is the issue you can define background in xml and use any number of time you want. – VikasGoyal Oct 01 '15 at 07:57
  • @pskink- it worked. thanks a lot. If you could paste your comment as an answer, I'll mark it as the accepted one. – Stralo Oct 01 '15 at 08:11
  • @pskink - I just looked at the view hierarchy and for some reason the background view has been added multiple times. I'm currently calling both addView(child, index) and child.layout(l, t, r, b) in onLayout. Any idea why this could be happening? – Stralo Oct 01 '15 at 08:17

0 Answers0