Normally, Compose automatically handles LTR and RTL layout directions. For example, Alignment.TopEnd
will be on the right for a LTR locale and on the left for an RTL locale. And, normally, this is what we want.
Sometimes, though, we need to override that behavior, for cases where UI positioning is based on other criteria (e.g., where the steering wheel is in a car). How can we do that?
LocalLayoutDirection.current
will be either Ltr
or Rtl
for the current locale. You can use that to choose things like Alignment
values that match your requirements. For example, this snippet has a Column()
be positioned on the left, regardless of layout direction.
Box(modifier = Modifier.fillMaxSize()) {
val panelAlignment = if (LocalLayoutDirection.current == LayoutDirection.Ltr) Alignment.TopStart else Alignment.TopEnd
Column(
modifier = Modifier
.fillMaxHeight()
.wrapContentWidth(Alignment.Start)
.align(panelAlignment)
) {
// rest of UI goes here
}
}