I am having issues with modifying a parent view after a child is drawn. I need data from the child after the onDraw method is called. I need to use this information to modify the parent view. So after the child is drawn, how would I go about modifying the parent view (drawing on its canvas)? I'm sorry that I don't have code, but I don't know how to implement what I'm asking (thus the question). Is this possible? Any help is appreciated.
All,
I took your advice Vee and I ended up doing everything in the child. Its not pretty, I basically grab the parent and traverse through the children getting the parameters that I need... So, that is the solution that I used. Just when you are rendering the child you can reference the parent and then pull all the information you need.
Related
I have a created a mapview with markers on it.
Looking at this picture below:
Grandparent is a filling View
Parent is my MarkerView
Child is a marker which is clickable
Parent has clipChildren(false) and thus the children are visible.
My problem is that the children are clickable, except for the part where Child 2 is outside the Parent.
Parent also has the appropriate TouchDelegate (and I also tried this for the children).
How can I make the complete child clickable?
I couldn't make it work without changing the elements.
I ended up enlarging the parent and using setTranslationY for the markers to keep them in place like this:
I had a similar issue and fixed it by setting app:elevation="XXdp" to the child.
The reason why you can't do this is that the default implementation of ViewGroup#dispatchTouchEvent(MotionEvent ev) iterates over the children (not the grandchildren) to look for a child that are a target (is on bottom of the click event point) and can receive touch events. It will find nothing if you touched the part that it is outside the bounds of Parent, which is the only child of Grandparent. If it does not find Parent, Parent will never be able to make a look up on its children (Children 1 and Children 2) and eventually dispatch the event to Child 2.
So you either increase the size of Parent (as the acceptable answer), which is the most easy way, or you will have to override the method ViewGroup#dispatchTouchEvent(MotionEvent ev) of your Grandparent to make a more complex lookup, like looking up grandchildren too. The method is already fairly complex, if anyone finds a implementation, please share because I don't have one.
I created a custom layout which can have an arbitrary amount of child views.
I figured out that the child views are not available when I try to access them in the constructor of my layout view.
So what I currently do is to access them in onMeassure, but it seems to be a bad idea, since this gets called several times.
What is the best place in my layout to init child views? I wish there was a method like onChildViewsAttached(). Any ideas?
Child views are attached to the parent once layout pass is finished, i.e layout() of the view group is finished.
You can also register OnGlobalLayoutListener or OnPredrawListeneron the ViewTreeObserver of your custom layout.
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.
Basically, I'm trying to figure out if there's any way for a parent layout to tell when a child has invalidated/is drawing/has changed.
So far, I've tried looking at almost all methods with 'draw' in them (i.e. onDraw, dispatchDraw, draw, etc) and none of them seem to fire if a child is quietly updating itself.
Thanks! :)
I think you want to set up a ViewTreeObserver:
http://developer.android.com/reference/android/view/ViewTreeObserver.html
i have to draw a ball inside a view (or anything else that is good for this task).
I have followed some tutorial about the matter, but every tutorial that i have found
uses only one view (that is shown on the screen without the use of a layout).
But in my Activity i use a layout, that is composed by many views, and i want to draw
only on one of them.
here a little mockup!
Anybody knows a way to do it?
Is the view the wrong container to do it?
Thanks is advance!
Bye
...
You should extend a view that inherits from ViewGroup. this kind of view lets you handle a view that contains other views. with that, you can control the drawing, measures and layout of each of it's children separately
Your mockup can be made with a LinearLayout, which will contain some TextView's and you own View (the one which is containing the ball).
I'm surprised by this question, because there are many examples over the net explaining how to build a layout containing multiple views.