0

Here is my Code ! ;) And my Box is aligned at the top left, but I want the box in the center. You know how I can do this?

OptIn(ExperimentalWearMaterialApi::class)
    @Composable
    fun OnTouch() {
        val width = 100.dp
        val squareSize = 50.dp
        val swipeableState = rememberSwipeableState(0)
        val sizePx = with(LocalDensity.current) { squareSize.toPx() }
        val anchors = mapOf(0f to 0, sizePx to 1) // Maps anchor points (in px) to states

        Box(
            contentAlignment = Alignment.Center, modifier = Modifier
                .width(width)
                .swipeable(
                    state = swipeableState,
                    anchors = anchors,
                    thresholds = { _, _ -> FractionalThreshold(0.3f) },
                    orientation = Orientation.Horizontal
                )
                .background(Color.LightGray)

        ) {
            Box(
                Modifier
                    .offset { IntOffset(swipeableState.offset.value.roundToInt(), 0) }
                    .size(squareSize)
                    .background(Color.DarkGray)
            )
        }
    }

thanks in advance ! ;) I did search the documentation but found nothing...

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841

1 Answers1

1

The alignment of the LightGray Box depends on the parent container.

For example you can use a Column like:

Column(
    modifier = Modifier.fillMaxSize(),
    verticalArrangement = Arrangement.Center,
    horizontalAlignment = Alignment.CenterHorizontally
) {
    Box(
        contentAlignment = Alignment.Center, 
        modifier = Modifier
            //...
            .background(Color.LightGray)

    ) {
        //...
    }
}
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • Do i can't say somwhat like box(align.center) ? I want the whole box in the middle so the gray and lightgray box are there aswell. Without putting another box colmun or row around my box to do this. – Kerbal Space Program Germany Jan 26 '23 at 13:13
  • @KerbalSpaceProgramGermany in this way the `Box`s are in the middle. What is the problem of using a parent container (as a `Box`, `Column`..)? – Gabriele Mariotti Jan 26 '23 at 13:21
  • Since it feels for me redundant. And somewhat tricked. Why i have to say please put a column there that places all of his content in the middle, instead of just saying please put the box with his content in the middle. – Kerbal Space Program Germany Jan 26 '23 at 13:36