6

How to calculate empty space in lazy column after last visible item in Jetpack compose.

enter image description here

3 Answers 3

5

You can use Modifier.onSizeChanged{intSize->} on your LazyColumn and its parent Composble and check the distance between them.

One thing to note here is it requires another recomposition after getting size of parent and list. So you might need do to a check or refer this answer to get dimensions without recomposition

How to get exact size without recomposition?

@Composable
private fun LazyColumnParentSizeSample() {

    var parentHeight by remember { mutableStateOf(0) }
    var listHeight by remember { mutableStateOf(0) }
    Column(modifier=Modifier.fillMaxSize()
        .onSizeChanged {
            parentHeight = it.height
        }) {

        LazyColumn(modifier=Modifier.onSizeChanged {
            listHeight = it.height
        }){
            items(10) {
                Text("Item: $it")
            }
        }
        Text("listHeight: $listHeight, parent height: $parentHeight, difference: ${parentHeight-listHeight}")

    }
}

To get value in dp you can use LocalDensity.current

    val diffInDp = LocalDensity.current.run {(parentHeight-listHeight).toDp()  }
Sign up to request clarification or add additional context in comments.

4 Comments

can you please add some sample code to understand better
Added sample code with Modifier.onSizeChanged
How to convert this height into DP ?
@TippuFisalSheriff updated answer. To convert to dp or convert from dp you can use LocalDensity.current.run{value conversion here}
2

Use Modifier.onSizeChanged{} to get the size of the LazyColumn

1 Comment

can you please add some sample code to understand better
2
LazyColumn(modifier = Modifier.onSizeChanged(size ->

int columnHeight = size.height
int screenHeight = //getyourscreenheighthere by using window class

int remainingSpacae = screenHeight - columnHeight

))

open this link to get screen height ->

Screen width and height in Jetpack Compose

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.