Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jetpack compose: Change layout direction of a Composable

I want to set a direction of a specific composable to be RTL


@Composable
fun ViewToBeChanged() {
  Row {
     Image()
     Column {
        Text("Title")
        Text("Subtitle")
    }
  }
}

Is it possible?

Jetpack compose Layout documentation mentions LocalLayoutDirection

Change the layout direction of a composable by changing the LocalLayoutDirection compositionLocal.

But I have no idea how to use it in a composable to take effect.

like image 436
Mahdi-Malv Avatar asked Mar 27 '21 19:03

Mahdi-Malv


People also ask

How are composable is redrawn?

When the state changes, the composable functions are called again with the new data. This causes the UI elements to be redrawn--this process is called recomposition.

What is composable in Jetpack Compose?

Jetpack Compose is built around composable functions. These functions let you define your app's UI programmatically by describing how it should look and providing data dependencies, rather than focusing on the process of the UI's construction (initializing an element, attaching it to a parent, etc.).

Is Jetpack Compose responsive layout?

Fortunately, Jetpack Compose comes with a bunch of tools to make it easier for developers to build these beautiful, responsive UIs that adapt onto any screen size.

What is Subcompose layout?

Subcompose layout which allows to subcompose the actual content during the measuring stage for example to use the values calculated during the measurement as params for the composition of the children.


1 Answers

You can use the CompositionLocalProvider to provide a custom LocalLayoutDirection.

Something like:

CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Rtl ) {
    Column(Modifier.fillMaxWidth()) {
        Text("Title")
        Text("Subtitle")
    }
}
like image 111
Gabriele Mariotti Avatar answered Oct 17 '22 06:10

Gabriele Mariotti