androidandroid-jetpack-composeandroid-compose-buttonandroid-jetpack-compose-button

How to align icon in button to the left and keep text centered


I am trying to align the icon of a button to the left and keep the text centered. Any ideas how this can be achieved?

My composable:

  @Composable
  fun CustomButton() {
    MaterialTheme {
      OutlinedButton(
        onClick = {},
        modifier = Modifier
          .padding(12.dp),
        colors = ButtonDefaults.buttonColors(backgroundColor = Color.White),
        shape = RoundedCornerShape(4.dp)) {
          Icon(
            imageVector = Icons.Default.FavoriteBorder,
            contentDescription = null,
            modifier = Modifier.padding(start = 4.dp)
          )
          Text(text = "Like", color = Color.Grey)
      }
    }
  }

This is how it looks now: enter image description here


Solution

  • if you need a full width button, wrap the content with a Box then add fillMaxWidth() and TextAlign.Center to the text

    @Composable
    fun CustomButton() {
        MaterialTheme {
            OutlinedButton(
                onClick = {},
                modifier = Modifier.padding(12.dp),
                colors = ButtonDefaults.buttonColors(backgroundColor = Color.White),
                shape = RoundedCornerShape(4.dp)
            ) {
                Box {
                    Text(
                        text = "Like",
                        color = Color.Gray,
                        modifier = Modifier.fillMaxWidth(),
                        textAlign = TextAlign.Center
                    )
                    Icon(
                        imageVector = Icons.Default.FavoriteBorder,
                        contentDescription = null,
                        modifier = Modifier.padding(start = 4.dp)
                    )
                }
            }
        }
    }
    

    otherwise you can create a custom layout

    @Composable
    fun CustomButton() {
        MaterialTheme {
            OutlinedButton(
                onClick = {},
                modifier = Modifier.padding(12.dp),
                colors = ButtonDefaults.buttonColors(backgroundColor = Color.White),
                shape = RoundedCornerShape(4.dp)
            ) {
                Layout(
                    content = {
                        Icon(Icons.Default.FavoriteBorder, null)
                        Text("Like", Modifier.padding(horizontal = 8.dp), Color.Gray)
                    },
                    measurePolicy = { measurables, constraints ->
                        val icon = measurables[0].measure(constraints)
                        val text = measurables[1].measure(constraints)
                        layout(
                            width = text.width + icon.width * 2,
                            height = maxOf(text.height, icon.height, constraints.minHeight)
                        ) {
                            icon.placeRelative(0, 0)
                            text.placeRelative(icon.width, 0)
                        }
                    }
                )
            }
        }
    }