Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to align row elements by the baseline in Jetpack Compose

How to align elements inside a Row by the baseline. My issue is that I want to have a Row element with multiple Text elements and each of the Text elements will have different font and size. By default, they are aligned on the top. I need them to be aligned on the bottom. This is the code:

class MainActivity : ComponentActivity()  {
    override fun onCreate(savedInstanceState: Bundle?)  {
        super.onCreate(savedInstanceState)

        setContent {
            MaterialTheme {
                Row {
                    Text(
                        text = "Abrakadabra",
                        style = TextStyle(fontSize = 22.sp, fontWeight = FontWeight.Bold)
                    )
                    Text(
                        text = "Abrakadabra",
                        style = TextStyle(fontSize = 14.sp, fontWeight = FontWeight.Bold)
                    )
                }
            }
        }
    }
}

Here is the rendered view of the code:

enter image description here

like image 886
Tigran Ghazinyan Avatar asked Feb 26 '20 01:02

Tigran Ghazinyan


People also ask

How do you center in jetpack compose?

To center align content of Column along horizontal axis in Android Compose, set horizontalAlignment parameter with the value of Alignment. CenterHorizontally . Also, we may fill the maximum width by the Column using Modifier. fillMaxWidth().

What is LazyColumn in jetpack compose?

LazyColumn produces a vertically scrolling list, and LazyRow produces a horizontally scrolling list. The Lazy components are different to most layouts in Compose. Instead of accepting a @Composable content block parameter, allowing apps to directly emit composables, the Lazy components provide a LazyListScope.

What is mutableStateOf in jetpack compose?

mutableStateOf creates an observable MutableState<T> , which is an observable type integrated with the compose runtime. interface MutableState<T> : State<T> { override var value: T. } Any changes to value will schedule recomposition of any composable functions that read value .

What is scaffold in jetpack compose?

Scaffold provides a slot for a floating action button. You can use the floatingActionButton slot and a FloatingActionButton : Scaffold(


2 Answers

In Jetpack Compose 1.0.0 it can be done in this way:

Row {
    Text(
        text = "One",
        fontSize = 20.sp,
        modifier = Modifier.alignByBaseline()
    )

    Text(
        text = "Two",
        fontSize = 12.sp,
        modifier = Modifier.alignByBaseline()
    )
}

Result:

enter image description here


If someone wants to align text to the last baseline when using multiline Text it can be done by using Modifier.alignBy(LastBaseline)

Row {
    Text(
        text = "Line 1\nLine 2",
        fontSize = 20.sp,
        modifier = Modifier.alignBy(LastBaseline)
    )

    Text(
        text = "Line 3",
        fontSize = 12.sp,
        modifier = Modifier.alignBy(LastBaseline)
    )
}

enter image description here

like image 55
iknow Avatar answered Oct 27 '22 12:10

iknow


UPDATE: This is no longer valid. Check @iknow's answer instead.

Currently, using Jetpack Compose 1.0.0-alpha03, you can achieve that with:

Text(modifier = Modifier.alignWithSiblings(FirstBaseline), ...)
like image 1
RickSanchez725 Avatar answered Oct 27 '22 12:10

RickSanchez725