# SnapPosition

Artifact: [androidx.compose.foundation:foundation](https://developer.android.com/jetpack/androidx/releases/compose-foundation) [View Source](https://cs.android.com/search?q=file:androidx/compose/foundation/gestures/snapping/SnapPosition.kt+class:androidx.compose.foundation.gestures.snapping.SnapPosition) Added in [1.7.0](https://developer.android.com/jetpack/androidx/releases/compose-foundation#1.7.0) Common/All
- Cmn

  ```
  interface SnapPosition
  ```
Known direct subclasses [SnapPosition.Center](https://developer.android.com/reference/kotlin/androidx/compose/foundation/gestures/snapping/SnapPosition.Center), [SnapPosition.End](https://developer.android.com/reference/kotlin/androidx/compose/foundation/gestures/snapping/SnapPosition.End), [SnapPosition.Start](https://developer.android.com/reference/kotlin/androidx/compose/foundation/gestures/snapping/SnapPosition.Start)

|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/gestures/snapping/SnapPosition.Center` | Aligns the center of the item with the center of the containing layout. |
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/gestures/snapping/SnapPosition.End` | Aligns the end of the item with the end of the containing layout. |
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/gestures/snapping/SnapPosition.Start` | Aligns the start of the item with the start of the containing layout. |

*** ** * ** ***

Describes the snapping positioning (i.e. final positioning after snapping animation finishes) of a given snap item in its containing layout.

## Summary

| ### Nested types |
|---|
| `object https://developer.android.com/reference/kotlin/androidx/compose/foundation/gestures/snapping/SnapPosition.Center : https://developer.android.com/reference/kotlin/androidx/compose/foundation/gestures/snapping/SnapPosition` Aligns the center of the item with the center of the containing layout. |
| `object https://developer.android.com/reference/kotlin/androidx/compose/foundation/gestures/snapping/SnapPosition.End : https://developer.android.com/reference/kotlin/androidx/compose/foundation/gestures/snapping/SnapPosition` Aligns the end of the item with the end of the containing layout. |
| `object https://developer.android.com/reference/kotlin/androidx/compose/foundation/gestures/snapping/SnapPosition.Start : https://developer.android.com/reference/kotlin/androidx/compose/foundation/gestures/snapping/SnapPosition` Aligns the start of the item with the start of the containing layout. |

| ### Public functions |
|---|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/foundation/gestures/snapping/SnapPosition#position(kotlin.Int,kotlin.Int,kotlin.Int,kotlin.Int,kotlin.Int,kotlin.Int)( layoutSize: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, itemSize: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, beforeContentPadding: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, afterContentPadding: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, itemIndex: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, itemCount: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html )` Calculates the snap position where items will be aligned to in a snapping container. | Cmn |

## Public functions

### position

Cmn Added in [1.7.0](https://developer.android.com/jetpack/androidx/releases/compose-foundation#1.7.0)

```
fun position(
    layoutSize: Int,
    itemSize: Int,
    beforeContentPadding: Int,
    afterContentPadding: Int,
    itemIndex: Int,
    itemCount: Int
): Int
```

Calculates the snap position where items will be aligned to in a snapping container. For instance, if `https://developer.android.com/reference/kotlin/androidx/compose/foundation/gestures/snapping/SnapPosition.Center` is used, once the snapping finishes the center of one of the items in the snapping container will be aligned exactly to the center of the snapping container, that is because the value returned by `https://developer.android.com/reference/kotlin/androidx/compose/foundation/gestures/snapping/SnapPosition#position(kotlin.Int,kotlin.Int,kotlin.Int,kotlin.Int,kotlin.Int,kotlin.Int)` was calculated as such a way that when one applies it to the item's current offset it will generate that final positioning.

The reference point is with respect to the start of the layout (including the content padding)

```kotlin
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.snapping.SnapLayoutInfoProvider
import androidx.compose.foundation.gestures.snapping.SnapPosition
import androidx.compose.foundation.gestures.snapping.rememberSnapFlingBehavior
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material3.Text
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

val state = rememberLazyListState()
val density = LocalDensity.current

// Illustrate using a custom SnapPosition that will snap to a static location (200dp) after
// the content padding.
val snappingLayout =
    remember(state, density) {
        val snapPosition =
            object : SnapPosition {
                override fun position(
                    layoutSize: Int,
                    itemSize: Int,
                    beforeContentPadding: Int,
                    afterContentPadding: Int,
                    itemIndex: Int,
                    itemCount: Int,
                ): Int {
                    return with(density) { beforeContentPadding + 200.dp.roundToPx() }
                }
            }
        SnapLayoutInfoProvider(state, snapPosition)
    }
val flingBehavior = rememberSnapFlingBehavior(snappingLayout)

LazyRow(
    modifier = Modifier.fillMaxSize(),
    verticalAlignment = Alignment.CenterVertically,
    state = state,
    flingBehavior = flingBehavior,
) {
    items(200) {
        Box(
            modifier =
                Modifier.height(400.dp).width(200.dp).padding(8.dp).background(Color.Gray),
            contentAlignment = Alignment.Center,
        ) {
            Text(it.toString(), fontSize = 32.sp)
        }
    }
}
```

| Parameters |
|---|---|
| `layoutSize: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The main axis layout size within which an item can be positioned. |
| `itemSize: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The main axis size for the item being positioned within this snapping layout. |
| `beforeContentPadding: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The content padding in pixels applied before this Layout's content. |
| `afterContentPadding: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The content padding in pixels applied after this Layout's content. |
| `itemIndex: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The index of the item being positioned. |
| `itemCount: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The total amount of items in the snapping container. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The offset of the snap position where items will be aligned to in a snapping container. |