Programmatic Control
snapToColumn
Use snapToColumn() to change the column count programmatically â useful for buttons, keyboard shortcuts, or accessibility.
val state = rememberPinchGridState()
// Zoom in / out buttonsButton(onClick = { state.snapToColumn(state.columnCount - 1) }) { Text("Zoom In")}Button(onClick = { state.snapToColumn(state.columnCount + 1) }) { Text("Zoom Out")}snapToColumn respects minColumns and maxColumns bounds, triggers haptic feedback, and fires the onColumnChanged callback.
Reading state
state.columnCount // current column countstate.minColumns // minimum allowedstate.maxColumns // maximum allowedstate.scaleProgress // 0fâ1f, progress toward next snapstate.isZoomingIn // true/false/null (gesture direction)state.previousColumnCount // for transition animationDisabling gestures
You can disable the pinch gesture while keeping programmatic control:
PinchGrid( state = state, gestureEnabled = false, // no pinch, but snapToColumn still works) { /* content */ }Scroll position preservation
When the column count changes (via gesture or programmatic), the grid snapshots firstVisibleItemIndex before the change and restores it after. For best results, provide stable key values to your items:
items(photos, key = { it.id }) { photo -> /* ... */ }