Should onLayout be overridden if there are no views inside - android

I have created a custom control which extends ViewGroup. Eventually, the implementation of the control is using only a canvas to draw some shapes on, without adding any Views.
Should onLayout be still overridden in that scenario with some sort of general implementation, or should I leave it an empty method?

Should onLayout be still overridden in that scenario
If you are extending ViewGroup then yes, you have to implement onLayout() even if it does nothing as it's abstract method. Alternatively extend i.e. FrameLayout instead of ViewGroup.
How would a general implementation be?
If you got no children to layout just keep empty method body.
EDIT
Yeah I meant if I should have some sort of a general implementation or just leave it empty
See docs:
void onLayout ()
Called from layout when this view should assign a
size and position to each of its children. Derived classes with
children should override this method and call layout on each of their
children.
There's no point for implementation if there's no use for it. There's no universal implementation that's why this method is abstract in ViewGroup. You got no childer then you not need to layout anything. Yet you need the
(empty) method due to extending abstract class.

Related

Android - difference between View.OnLayoutChangeListener and ViewTreeObserver.OnGlobalLayoutListener

According to the doc, the first is
Interface definition for a callback to be invoked when the layout bounds of a view changes due to layout processing.
and the second is
Interface definition for a callback to be invoked when the global layout state or the visibility of views within the view tree changes.
///
But they seem pretty similar to me. I was even able to use both of them interchangeably. Can someone give me a practical example of the usage of them? Thanks
An OnLayoutChangeListener is a listener for a specific View and will trigger only when that View goes through a layout pass (i.e. onLayout() is called).
An OnGlobalLayoutListener watches the entire hierarchy for layout changes (so registering one of these on any View in a hierarchy will cause it to be triggered when any View in that hierarchy is laid out or changes visibility).

Behavior of setWillNotDraw(false) in View

I am new comer to the android and got stuck with this concept.
I have extended FrameLayout and added child views. My child view's onDraw is called even without setting setWillNotDraw(false). Then what is the actual need of it and when this flag should be set.
setWillNotDraw is not required in a View class.
However, it is usually set to true in a ViewGroup. Therefore, is disables the onDraw method there.
If you need the onDraw method to be called inside a ViewGroup, you just set the flag to false like so: setWillNotDraw(false).
Also, if you don't want that onDraw in your view's subclass to be called, you have to call subclassInstance.setWillNotDraw(true)

Children of my custom layout won't re-layout and re-measure correctly

I have implemented a custom layout that extends ViewGroup. In its onLayout() method, I make a loop on all my children : I add them to the layout, measure them and then layout them. It works just fine.
But now I want my children to have dynamic content. So I have added a handler and a runnable to the viewholders of each of my children (my layout is populated with an adapter). This runnable aims at updating a textview every 10 seconds. What happens is that the TextView is truncated. I have found out that it actually takes the width of the first string it is set to and will never re-measure or re-layout again. I have decided to make my custom layout extend FrameLayout. And if in my onLayout() method I do call super.onLayout(), everything works just fine. Each time the TextView in the children is updated by the Runnable, I am called back on the onLayout method of my custom layout.
The big question is why ? Extending FrameLayout is not enough, I have to call super.onLayout(). I have checked the code of this method and can't figure out what it does to make it work. As layout is one pass top-down according to documentation, why calling this method makes my custom layout to be re-layouted each time a children is updated ? Please note that I have tried to call requestLayout() and invalidate() on my children to no avail.
Thanks for reading me. I can provide code if it is not clear enough.

Why is onLayout in custom ViewGroup called again and again?

I've a custom ViewGroup, to which a custom ImageView is added as a child. Now, my problem is that whenever I change the image in the child, by overriding the onDraw(), the onLayout method in the parent is called. Is it not possible to just change the image in the child without calling the onLayout method in the Custom ViewGroup? Thanks.
Interestingly, no one answers this simple question.
The onLayout() method is called by default when the viewgroup needs to be redrawn.
see https://developer.android.com/guide/topics/ui/how-android-draws.html
you can actually extend the viewgroup to implement your custom onLayout() and onMeasure() method to control the behavior it is called.

Can anyone give a good example of a Custom ViewGroup containing a Custom View with measure and layout examples? (In Java)

I've been having a few issues with using custom viewgroups and custom views. I've done a lot of searching already for this, but if I missed something feel free to link me there. Yes I have read everything about Views, ViewGroups, measure and layout passes on the android dev website, but I'm still confused without any Java sample code.
1)My understanding is that child-views need to implement onMeasure and onLayout. Does this mean that the parent ViewGroup needs to manually call measure() and layout() for each of its children? If so, where should these calls be located? Should they be in the Parent's onMeasure and onLayout methods? If so, where are the very first measure() and layout() calls that start this whole recursive process.
2) I see that layout params of a childview can be set using something like the following. Should this code be place inside the onLayout of the parent viewgroup?
CustomView cust = new CustomView(context);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
cust.setLayoutParams(params);
If I want to set a MeasureSpec, do I just add it as another rule here?
3) Is it okay to create an instance of a CustomView and add it to CustomViewGroup from inside the code for CustomViewGroup, and if so should I put this code in the CustomViewGroup constructor? If not, is it just understood that you create ViewGroups and then add children all from a different outside class?
4)Is it common to practice to call draw() for each of my childviews inside the onDraw() method for CustomViewGroup?
Right now, when I execute some code to draw a circle, it executes the draw method of view but nothing actually appears on the screen. When I add it to a relativeLayout instead of my customViewGroup, I have no problem...So I think I'm missing a key piece about how to get viewgroups to show up. (Yes, I set the ContentView to the ViewGroup).
Thanks in advance!
Edit: Please don't show me XML, I'm doing this all in Java.
I recommend you to read these slides and watch this video.

Categories

Resources