Martyn
Martyn

Reputation: 16622

Android - onDraw not being called in dynamically inserted custom view

I have a custom view being dynamically inserted and the onDraw is not being called. I initially made the custom view without the dynamic insertion and it worked fine, however when moving it to my working code base, I cannot get onDraw to get called ever, even though the view constructor gets called and the onMeasure also gets called. I've tried putting invalidate() and postInvalidate() calls around the place, but they don't seem to force the view to draw. The view hierarchy looks the same for both version and I'm lost.

Can anyone help?

EDIT: A bit more testing has shown that onDraw is getting called on the emulator, but not the handset. Also onSizeChanged on the handset has the correct values coming through, but on the emulator the values are all 0.

EDIT: I've been playing about with it and still can't get anywhere, so have put a bounty on it. If you need more code / xml / info then please let me know.

Upvotes: 3

Views: 2165

Answers (2)

Martyn
Martyn

Reputation: 16622

I changed the View to a TextView and it seems to work now, although I'm a little confused as to why the View didn't work.

Upvotes: 1

bigstones
bigstones

Reputation: 15267

This sounds a lot like a problem i had recently, with my first custom view. Did you notice if, when loading view hierarchy, your custom views show up? and anyway do they appear laid out in the wireframe preview or not?

If the latter is the case, it's requestLayout() that doesn't get called (loading view hierarchy calls that on every view - that's why it makes views show up). i've also found that a similar bug was reported already here.

Upvotes: 1

Related Questions