I have a ScrollView that I would like to have a variable number of children in. Easy game, use LinearLayout.
Now, I am looking for a way to make the widths of the children some proportion of the ScrollView itself (it is going to be placed in another auto-laid out view).
I have tried using LinearLayout weights (and weightSum) to keep them proportional to themselves, but then they just fill the ScrollView and don't allow scrolling.
I have android:fillViewport="true" set on the ScrollView so that it fills its container, but I can't seem to find a way to make the children some fraction of the ScrollView itself.
Basically I want ALL the children to have a fixed width of say a 3rd of the available space, so that while scrolling at most 3 items are shown, and they are proportional to the available space.
My thoughts are that I will need to create a CustomScrollView that extends ScrollView, and then #Override the onMeasure() method, is this the way, or is there an easier, more elegant solution (hopefully using the layout_weights engine already in LinearLayout)?
Any help would be much appreciated!
P.S. I had a beautiful picture drawn, but apparently you need 10 rep, lame to the n^th degree. I would love to have contributed to more questions on SO, but I need like 15 rep to vote up a question that helped me? That means I need to ask 15 questions, of which I can already find all the answers on here.
Related
I want to have this layout in my Android app.
Requirements:
All rectangles should distribute evenly over the entire screen
The two rectangles on the bottom right should have the same height (so also distributed evenly)
I tried GridLayout but that won't let me stretch the children across the screen. I tried RelativeLayout without success. I tried nesting LinearLayouts with weights, it works, but I read that nesting weights is bad for performance. And finally, I tried TableLayout with weights, but it does not have something like "row span" like an HTML table so I cannot do the two rectangles in the bottom right corner like I want them to be (without nesting weights again).
Somebody, please help! How hard can it be? I don't know what to do here. I could implement my own ViewGroup or something to manually implement the layout. But it feels like this is something very simple, so I should be able to use standard layouts.
You are able to use GridLayouts to do such a thing, for some help fitting the screen I suggest you look at the following: How to make a GridLayout fit screen size.
Depending on what you are doing, nested weights with a series of Linear Layouts will not harm your performance too much, however it is is something extremely large like an intricate game or something of that nature I suggest using the GridLayout.
hi i've some problems with listview. My list do have very different cell heights, some of them are even bigger then the height of the screen. What I found out, that listview scrolling intepolator has problems with calculating the inertia. each time when there is big height difference between two cells currently switching in the visible area, the scrolling junks..I've looked into traceview and there actually is not that much of a delay. (there is no line tallet then the 16ms treshold, though there is visible difference), that means it actually does not junk and it is rather only quickly accelerating and deccelerating the scrolling speed. (it occurs in exactly the same time as the scrollbar height changes)
Does anybody know how to solve this? I've read an article from facebook and they apparently had the same issue..There is written they needed to write their own item recycler. I'm kind of aware of that and would like to see some simpler solution..
Sorry for this question being so abstract, but I guess any code is not necessary..
thank you for your help..
ListViews recycle, the views that have been already inflated. Since in your case the Listview has items which vary in height, it is better to write your own recycler.
If you have specific categories as in X, Y and Z then you can check the layout type by adding an attribute to the layout as a tag, and inflating conditionally. But the best way to do this is write your own recycler.
When I look at several of the more popular android walpaper programs, (HD Wallpapers and the like), they all seem to have a very similar layout with 4 pictures in a 2 x 2 grid where each takes up approximately 1/2 of the parent's respective width and height. After not having a lot of success emulating this with various combinations of layouts and objects, I am trolling for a pointer. Anyone who could get me pointed in the right direction would be greatly appreciated. Is there a standard mix to achieve this sort of view?
Side note: Please be gentle, it's my first time... asking a question. ;)
You could use a GridView with android:numColumns=2.
You can see a GridView example here. You would set the height by manipulating the LayoutParams on the views that you put inside the GridView (via its Adapter).
If that doesn't float your boat and you're not doing much scrolling, you can always create a TableLayout with TableRows of 2 Views apiece, or a vertical LinearLayout containing two horizontal LinearLayouts of 2 Views apiece.
What have you tried already? Can you expand on your use case a little bit more? Do the images need to scroll? Can they be static? Will there be more than 4 images on a page?
I am trying to write a calendar app and I plan to have a grid (week view) which will probably be a TableLayout and directly on top of that I will have to absolutely position events on the grid.
But AbsoluteLayout is deprecated. What should I use instead?
Events may overlap and I think it would be really silly to try and use a non absolute layout to achieve what I want.
Maybe I should use RelativeLayout with a margin left and margin top on each of the child nodes. Seems odd to do it that way and might not be as efficient. Is this the best way or is there an alternative?
But AbsoluteLayout is deprecated. What should I use instead?
Write your own layout manager that implements the rules you want.
CommonsWare suggested writing my own layout manager and Christian pointed out it sounds easier than it is.
Yahel suggested SurfaceView/OpenGl and draw whatever I want.
I found out you can recreate absolute positioning by adding your child views to a RelativeLayout and set the RelativeLayout.LayoutParams to have only default values except width, height, marginTop and marginLeft. The top and left margin will be similar to top and left in AbsoluteLayout. Also, negative margins are supported.
Make sure you account for screen density and width and orientation changes and all the other caveats of absolute positioning that used to apply to AbsoluteLayout
If you have problems with getting your content to overflow past the right edge of the screen, try supplementing your positive left margin with an equally negative right margin. (original question)
I would suggest either going the easy way :
Setting your calendar for 15(or 30) minutes intervals, this way you don't need absolute positionning. A table view filled with linear view each representing 15 minutes interval and fill these with events.
Or going the hard but a lot more stable/speedy/customisable way :
SurfaceView/OpenGl draw your own however you want.
The problem with number 1 is the fact that the more you add elements in your view hierachy the more your app will take a performance hit. Say a conventional month you have 3 appointments a day, your hierarchy will be filled with a hundred views wich will be very long to render and heavy memory-wise too.
The problem with number 2 : Well it's a lot harder to code at first. If you have to write your own layoutmanager, don't, go surfaceview or openGL.
I need to have a linear Layout whose height is wrap_content,
but how can I specify a child of this linear layout to stretch to its parent?
I tried
... some other children ...
But this does not work. The image does not scretch.
wrap_content will stretch it to however big the content inside it is.
fill_parent will stretch it to the size of the parent container.
I would try fill_parent for the child.
android:layout_width="fill_parent"
android:layout_height="fill_parent"
This should fill the parent with the child.
If you have multiple children within the parent and wish to leave no free space in the parent you should use
http://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html#weight
android:layout_weight" is the factor
the view will be stretched, when the
parent is larger than its childs in
sum.
well i think this would not make so much sense.(i don't mean anything rude)
Your child layout can not stretch out to the parent's size because the size of the parent layout is only as big/small as your child class could- attempting to wrap around however big/small the child could get. Therefore, it's just basically like two people who can't decide who should decide. The other waits for the other's decision, and vice versa. Those layouts are dependent on each other's size, when the problem is there is not an established size to relate to then move on. You could maybe, reconsider your logic dude. ^^ I got so confused that by formulating an answer i got lost in confusion myself.
I hope this helps. This might help beginners in the future. This seems to be a fairly old question left unanswered.