Create custom View group(Diagonal Layout) - android

I want to create a custom diagonal layout like androids predefined linear layout..so i started with a class extending ViewGroup and overrided OnLayout and OnMeasure methods..However I am finding it difficult to understand where to write the logic for alligning all the Views such that they will be placed diagonally
I read few blogs on it How to Create Custom Layout in Android by Extending ViewGroup Class
Also followed the google I/O video
But still not clear about how to get started with creating a diagonal layout..can someone suggest any additional resources in this regard..???

The logic to measure children should be in onMeasure. In here, you want the sum of all children height to be as big as the height given in the heightMeasureSpec. Same goes for the width.
To do so, you can divide the width and height from measureSpec by the number of children, and call the child views measure method with those computed values.
The logic to position the children is in onLayout. In here, you call the child views layout method one after another, passing it positions (left, top, ...) incremented after layouting each child.
You must take care of child views margins in both methods, as well as your container padding.
Lucas Rocha has a very good article (with lot of reverse engineering) for custom views.

Related

Custom View onMeasure vs onSizeChanged vs onLayout

I have implemented some custom views in Android and one of the most frequent problems I have is to adapt the layout to the screen, weather by matching the parent's content, MATCH_PARENT, or wrapping its own content, WRAP_CONTENT. The ConstraintLayout, for instance, does not support WRAP_CONTENT.
The trouble starts when I have to adapt the children size, which initially I was doing on onLayout. One use case of this is when I want to have children which width and height respect a certain aspect ratio; and the width grow as much as possible.
So, in my onLayout, if changed was true I had reset constraints reset. However, it was causing a loop, since it changes the layout again.
What would be the proper place to size the custom widget children?
onSizeChanged or onMeasure?

How does Android achieve the attribute "wrap_content"?

These days I am learning how to customize the view on Android.I know if we set the minimum width, then the system will compare the min width we set and the measured width and choose the proper one to fit the view.What I am confusing is that how does android system change the size of the view according to the content of the view as the word "wrap_content" means.I want to know more details about how to achieve "wrap_content". Thanks a lot.
This is what Official Documentation says (I am adding just a part of it for a quick read)
When a View object's measure() method returns, its getMeasuredWidth() and getMeasuredHeight() values must be set, along with those for all of that View object's descendants. A View object's measured width and measured height values must respect the constraints imposed by the View object's parents. This guarantees that at the end of the measure pass, all parents accept all of their children's measurements. A parent View may call measure() more than once on its children. For example, the parent may measure each child once with unspecified dimensions to find out how big they want to be, then call measure() on them again with actual numbers if the sum of all the children's unconstrained sizes is too big or too small (that is, if the children don't agree among themselves as to how much space they each get, the parent will intervene and set the rules on the second pass).
The measure pass uses two classes to communicate dimensions. The ViewGroup.LayoutParams class is used by View objects to tell their parents how they want to be measured and positioned. The base ViewGroup.LayoutParams class just describes how big the View wants to be for both width and height. For each dimension, it can specify one of:
MATCH_PARENT, which means the View wants to be as big as its parent (minus padding)
WRAP_CONTENT, which means that the View wants to be just big enough to enclose its content (plus padding).
MeasureSpec objects are used to push requirements down the tree from parent to child. A MeasureSpec can be in one of three modes:
UNSPECIFIED: This is used by a parent to determine the desired dimension of a child View. For example, a LinearLayout may call measure() on its child with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how tall the child View wants to be given a width of 240 pixels.
EXACTLY: This is used by the parent to impose an exact size on the child. The child must use this size, and guarantee that all of its descendants will fit within this size.
AT MOST: This is used by the parent to impose a maximum size on the child. The child must guarantee that it and all of its descendants will fit within this size.

get size of parent layout/fragment while drawing it

I have a custom view that I'm using canvas to be able to draw on it what I want, this view inside a scrollview that take match_parent for each width and height and also my custom view take match_parent for each width and height
I want to know the size of the scrollview to be able to draw only inside the available space that I have
I have tried onLayout, onMeasure but nothing works with me
Can any one please help?
I have solved this problem using
onGlobalLayout Method, and I also inherit Viewgroup not View and implement onLayoutMethod, to make element shown in my fragement
Thanks all

How can I get a Layout "ready drawn and attached" callback?

i have a big problem with a layout in my Android Application. I have a
RelativeLayout
- inside this Layout I will nest a TableLayout (at Top) and another RelativeLayout (at Bottom)
In my program I fill both layouts (programmatically) with some views - first the TableLayout and then the RelativeLayout. I need the height from the nested RelativeLayout to calculate some things. But I always get the height without the height from the "fresh painted" TableLayout.
How can I get a callback (ore something) to recognize that my first layout (TableLayout) is finished and "sticks" at the parent layout? Then I determine the correct amount remaining height (of the nested RelativeLayout).
Probably you need to override the class TableLayout and override it's methods onMeasure, onDraw or onAttachedToWindow
It's not difficult, but a bit tricky to handle when a view is completely drawn on the screen
Check the doc: http://developer.android.com/reference/android/view/View.html#onMeasure(int, int)

How Android Draws Views

In How Android Draws Views:
"Because the tree is traversed in-order, this means that parents will be drawn before (i.e., behind) their children, with siblings drawn in the order they appear in the tree."
Does the author actually mean "pre-order", instead of "in-order"?
Thank you for your help.
It's more like "depth-first order" for measuring the children and "pre-order" for the actual drawing.
The parent will be drawn first and the children stacked on top of the parent.
That is a bug in the documentation. It should be pre-order traversal where the parent draws before children.
Most UI toolkit libraries (including android) do this:
render the view hierarchy top down in pre-order - first draw self,
then children
Measure (size calculation) happens bottom up in post-order - first
measure children, then measure self
Layout (positioning) happens top down in pre-order - first my parent
positions me, then i position my children

Categories

Resources