Compose: LazyVerticalGrid with variable contentPadding - android

I want to display a LazyVerticalGrid with three columns. At the top, I want a header that spans all columns and scrolls with the content. This works fine using an item at the beginning that sets the span to maxLineSpan.
However, I want the main content of the grid to have a contentPadding of 16 dp. At the same time, the header is supposed to be full-bleed, i.e. the contentPadding should not apply to the header.
What is the best way to achieve this with LazyVerticalGrid? Manually adding start/end padding to the content items based on the column they're in seems to be not an option, as the width of the item in the middle doesn't scale proportionally.

Related

Jetpack Compose: How can I align the height of the items of the first column in a row depending on the height of the second column?

I want the items of the first column to stretch their height depending on the total height of the other column in the row.
Now I have it:
And I want it to be like that:
Is this possible without hardcoding the height through calculations?
Use compose arrangement 'equal weight' according to this documentation link
Modifier.weight()

Get Jetpack Compose FlowRow to produce a masonry layout

Android has a view-based layout for achieving a masonry style layout where elements that could fit in a row would also evenly consume the remain row space:
I'm trying to replicate this with the Accompanist FlowRow, but as I browse the source, I don't think it was written to perform the extra measure and resizing necessary to adjust it's placeable elements. Seems the best I can get from it is:
Anyone have an example of getting FlowRow modified to layout in this way? I just started browsing source to perform custom layouts, so a pointer or two would help.
I think the algorithm would be something like:
Get the parent container width
Get the width of each item where its width is set to wrap its content
Take n items where the total width of n <= parent width
Add the remaining row width (width/n) to each item's width
Set the x, y positions of those items
Repeat starting with n+1 item

android, how to set a same height for my cards without hardcode in lazy row

I have a lazy row with items that have wrap content height.
my problem is that some texts of these items is visible or invisible base of that item. so the height of that cart is not fix and it will change.
how can I find the max height of that item (with all texts) and set that height to all items.
I do not want to set a hard code height to my items (like 300.dp)
as you can see in this image: the below button change its position based on the card's height.
I want that button fix in its place and not move up and down.
https://i.stack.imgur.com/R0Tc6.png
how can I fix that problem?
Have you tried experimenting around onGloballyPositioned{..} modifier property?
val localDensity = LocalDensity.current
Text(modifier = Modifier
.onGloballyPositioned { thisText ->
with (localDensity) {
thisText.size.height.toDp()
}
},
text = "Text"
)
Edit: Intrinsic Measurement is not allowed in LazyList components, have a look at this post, looks like similar to yours.
Jetpack Compose: Row with all items same height
Also, Constraintlayout maybe a good thing to experiment on.
One of the rules of Compose is that you should only measure your children once; measuring children twice throws a runtime exception. However, there are times when you need some information about your children before measuring them.
If you have a look at the Android documentation for Intrinsic Measurements, you will have a clear idea of what to do. In your scenario, you need to force Compose to measure the size of your children to adjust the parent composable's dimensions accordingly.
This medium article gives an easier example on how to use IntrinsicSize in Compose.

How to set multiple scroll views size to change dynamically relative to each other?

I have two scroll views in a vertical linear layout.
I want them to be relative to each other so that they fill the entire linear layout and compensate if one cant cover half the screen.
Lets call that scroll views TOP and BOT.
If the screen can display 4 rows and both scroll views have infinite rows, each scroll views should display 2 rows and be able to scroll down to se the rest rows.
If TOP has 1 and BOT infinite rows, BOT should be resized to 3/4 of the linear layout.
If TOP have infinite and Bot has 1 row TOP should still just display 2, i.e. it should never pass the linear layouts vertical center.
Here are some pictures for reference:
my setup with weight set to 0.5/0.5.
result of 0.5/0.5 weight. Notice the gray bar above the BOT title bar. This empty space should be filled by the BOT bar.
if Using fixed size or wrap content the TOP will push the Bot out of view.
How can I have them hugging each other and still set TOP to a maximal height?
Preferable in XML.
Its better to set the weight dynamically. Count the number of items in both views. Set the weight of each view according to the ratios of their number of items. You can refer set weight dynamically for setting weights at run time

android GridLayout divider row

I'm trying to implement a tabular layout that has a header and a bunch of rows underneath it. I've chosen the GridLayout (android.support.v7.widget.GridLayout) as there's some requirements for some elements to span multiple columns (but those are of no concern to the question).
My header cells each contain a LinearLayout with a bunch of TextViews, they're dynamically filled in code,for the sake of example, have a look at the image below.
The second row should contain the divider which is a simple view, that should span my header columns (3).
The problem is the width of the divider - if I choose MATCH_PARENT, it will push the GridLayout to fill the whole remaining space to the right. The grid needs to wrap the content and center itself horizontally. It seems to me there's a conflict between the grid's layout (WRAP_CONTENT) and the divider's layout (MATCH_PARENT).
How can I fix the width of the divider without hardcoding it?
http://i61.tinypic.com/2415eg5.png
In red, my LinearLayouts (header), green, the GridLayout itself, the thin blue line at the bottom is the divider.
Thanks,
MO
SOLUTION (as provided below):
I had to set the column weight for the divider to 1, without specifying a width (actually setting it to zero). Because of my specific requirement to handle all of these in code, the solution was to manually instantiate the GridLayout.LayoutParams class and use
ColumnSpec = GridLayout.InvokeSpec(row_index, num_spanned_cols, weight)
Hope this helps others in the future.
If you set the width to 0dp then give it android:layout_weight="1"
(you could give it any weight you want) it should fill all the available space and not push your bounds if I understand what you are asking correctly

Categories

Resources