Search code examples
androidandroid-layoutandroid-jetpack-composeandroid-jetpack

How to make a text right aligned?


I have two texts in a column and I want the second to be right aligned:

first
                                                 second

How can I do that?

I've tried wrapping the second text in a Row or a Column, to no awail. This is what I have so far:

    Column(modifier = Modifier.fillMaxWidth()) {
        Text(text = "first")
        Column(horizontalAlignment = Alignment.End) {
            Text(text = "second")
        }
    }

Solution

  • Please try as follows:

    Column(modifier = Modifier.fillMaxWidth()) {
        Text(text = "first")
        Text(text = "second", modifier = Modifier.align(Alignment.End))
    }
    

    And your code snippet didn't work because the inner column's width was effectively set to 'wrap content.' Adding modifier = Modifier.fillMaxWidth() there would have led to the result you expected.