I am creating a custom view for my Android app that should replace a huge Listview with custom cells. My cells used a RelativeLayout and layout_height="match_parent"
Now I wonder how the ListView knows how high each cell needs to be because it does obviously not fill the parent. So how does it determine how large the distance between the top and bottom elements of the RelativeLayout is.
Explaination:
If you are just wandering how does the listview know the cell's sizes, it is thanks to her onMeasure() method
As you can read:
Measure the view and its content to determine the measured width and
the measured height. This method is invoked by measure(int, int) and
should be overridden by subclasses to provide accurate and efficient
measurement of their contents.
In shorts, with this and other methods, the ListView can measure the view's sizes and it will resize the view itself based on those measurements.
I hope this helped, if I misunderstood, please let me know
Related
I have one list view and one expandable list view and i want to show both on same view (I can fix the height of list view but that will scroll my list inside that defined height only).
i have these below link so far but by using this method it allocation blank space although it is allocating dynamic height.
http://kk-brothers.blogspot.in/2011/09/dynamically-change-listview-height.html
Please check padding/margin also get place in calculation. Conversion of dp into px also required. But I will request not to design like this. It will destroy recycling property of your listview/recyclerview.
Folks,
I am working on a custom view that displays images as a grid:
class MyImageGridView extends LinearLayout {
....
public addItem(String imageFileName);
...
}
From MainActivity's onCreate method, I call addItem for all the images I need to display.
At some point, I need to create child controls. But first, I need to calculate the width of the control. This width is based proportionately on the width of the parent control. For example, if the width of MyImageGridView is 600 and I decided to show 3 images per row, the width of my child control would be set to
600/3=200 pixels.
The width of the parent control also determines the number of child controls I will eventually have. For each row in the grid, I end up creating another LinearLayout control.
I figured I could do all this in onSizeChanged event of MyImageGridView. However, it appears I cannot really create any child controls in onSizeChanged. Eclipse just locks up.
I cannot really create the controls in onCreate as the width of the control has not yet been determined and therefore I won't really know how many child controls I will end up creating.
I am wondering if anyone has a better idea on what would be the ideal event to create the controls dynamically.
Thank you in advance for your help.
Regards,
Peter
In this case you can probably do it during onLayout which will supply dimensions determined from onMeasure. It sounds like you might not need to override onMeasure in your case, so that keeps it simple. You need to be cautious with adding views during onLayout the android framework provides a few specialised members for doing this.. see addViewInLayout..
I want to stretch the row of a GridView to match the height of the screen in Android.
How do I do this?
You are in control over your row heights, by virtue of what you put in
them. Since your cells appear to have more than one widget, they are
presumably wrapped in a LinearLayout or something. Set your
LinearLayouts to be some specific height, and the rows will all be
that height.
Personally, I think you should be resizing your images if you are
going to have text above and below each image on a per-cell basis.
With the Reference commonsware link
I have a list view which is rendered each time with a different list of items, with variable height.
What I want is to set all the Views in the list view to the same layout height according to the view with the highest layout_height, when setting it to layout_height="wrap_content" for each View.
Also I would like to apply min and max values for the height.
So if I define min=30dp,max=100dp, and the biggest View is automatically rendered with 70dp
all Views in the ListView should be set to 70dp.
I have no idea how to go about it, expect for calculating in the code the max view height values, and setting them to all views, but this doesn't seem very elegant to me, especially as I need to translate it to DPs in the code.
Any simpler ideas?
Your right what you try to achieve wont be as pretty as the regular way, but it'll work ;) usually you would decide height beforehand and then go with it :)
I need to calculate the measured height of my views without measuring all views, because these views contain images fetched from the internet. When I measure the views, they download images to memory and I an catch out of memory exception. I use custom ScrollViews, which work like ListViews, display some part of views and when scroll, add to bottom or top.
How can I calculate the height of all my views without throwing out of memory exceptions?
Or how can I make ListView calculate its height before rendering views?
You can use getLeft(), getTop(), getRight() and getBottom() these method's on view. for more information . see following link
http://developer.android.com/reference/android/view/View.html
You answered your own question:
display some part of views and when scroll, add to bottom or top.
ListView does NOT know the heights of items it has not yet seen. It lays out some or all of the items it knows, adds an extra bit of UI at top and bottom to visually indicate that there may be more items, and scrolls that. The total scrollable range for ListView can change as it adds more items.