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)
Related
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?
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.
When I have buttons on the xml page, moving or enlarging them is very frustrating because i can't make them go where I want. Sometimes if I want to move something to the right it'll instead move somewhere else randomly or nothing will happen. Is this because of the relativelayout? And what's the difference between relative and linear layouts?
1) RelativeLayout - Views inside a RelativeLayout are relative to the position of other children in the RelativeLayout.
2) LinearLayout - Views inside the LinearLayout are in a linear pattern either horizontal or vertical.
I personally rarely use RelativeLayout unless it is inside a LinearLayout and I need to position a single view in an odd place against the horizontal or vertical aspect of LinearLayout. RelativeLayout can be confusing because if you change the dimensions of one view in it you have to adjust the dimensions of all other views that are relative to that view.
In framelayout, if I want to put a view in the position that starts from 1/2 of the screen width. How can I do it? Thanks.
You can accomplish that in multiple ways. It only depends if you really need to stick with the FrameLayout.
Two easiest would be those:
FrameLayout - you would need to know whats the width of the layout (so if your layout is wrap_content it might get tricky). Set your view's gravity to left, and set its margin_left to half of the layout's width.
LinearLayout - either put it inside your FrameLayout (not very good practice), or (if you can) swap it with your FrameLayout. Set it's orientation to horizontal, add dummy view with width = 0dp, weight = 1. Then add your view with the same values for width and weight.
I have a layout like this (abstracted):
Scrollview - fill_parent
LinearLayout - wrap_content
ImageView 1 - wrap_content
ImageView 2 - ...
ImageView 3
...
In the course of user interaction some images are replaced by larger or smaller ones, shortening or lenghtening the area to be scrolled.
And that is the problem. The Scrollview (SV) does not know about the change, so either it scrolls over a lot of empty space at the bottom, or cuts off a picture or two at the bottom.
Question 1: Can I somehow make the SV readapt to the changed hight of the LiearLayout (LL)?
Question 2: I can obtain the current size through getHight on the LL. But supplyong it to the SV via changed LayoutParams does not work - of course, that would only change the height of the SV on the screen. Is there a way to put the changed height of the LL into the SV in the code, somehow?
Question 3: I havn't tried it yet. Would creating the SV, LL and its children in code and then adding/removing children of changed size as required, make the SV adapt to the changes?
And last question: Is there a better aĆ¼pproach, except using ListViews?
Have you tried calling requestLayout() when images change?