androidandroid-jetpack-composecenteringtext-alignmentandroid-compose-button

Fix Text position in center of Jetpack Compose Button


I'm currently working on a button, which has 3 elements: An icon (with a fixed size), a title (f.e Buy Now!) and the price of the item. The price which should be displayed is adaptive, this could be €2,00 or €2000,00. The title is supposed to be centered, based on the Button itself, rather than the area it can occupy.

The price of object has the priority within the button, and should always be fully displayed with a set style. Due to this, the size of this object is variable, and can not be determined beforehand.

When the length of the price object increases, naturally the available space of the title decreases. However, when attempting to center the text, I could only get it to center based on the available space, which resulted in the text being off-center. Rough wireframes

How could one approach this issue, allowing for the text to be centered based on the parent (button), rather than the available text size?


Solution

  • You can try this:

    Button(
        modifier = Modifier
          .wrapContentHeight()
          .padding(horizontal = 8.dp),
        onClick = {}
    ) {
          Row(
              modifier = Modifier.fillMaxWidth(),
              verticalAlignment = Alignment.CenterVertically,
              horizontalArrangement = Arrangement.SpaceAround
          ) {
    
               Box(
                   modifier = Modifier.weight(1f),
                   contentAlignment = Alignment.TopStart
               ) {
                   Icon(
                      imageVector = Icons.Default.ImageSearch,
                      contentDescription = null
                   )
               }
    
               Box(
                   modifier = Modifier.weight(1f),
                   contentAlignment = Alignment.Center
               ) {
                   Text(
                         text = "Buy Now"
                   )
               }
    
               Box(
                    modifier = Modifier.weight(1f),
                    contentAlignment = Alignment.TopEnd
               ) {
                   Text(
                       text = "€ 2.00"
                     // text = "€ 2000.00"
                   )
               }
           }
     }
    

    enter image description here

    The Button has a content parameter you can use to set its content, in this case we use a Row to set contents in the horizontal axis.

    Note that each of the components, Icon Text and Text are wrapped inside a Box with a weight of 1f, making each those boxes as their container that also takes equal divided space of the parent Row.

    The middle Box positions its child in the center, while the first and last Box positions their children (i.e Icon and Text) in TopStart and TopEnd alignment, though you don't need to worry the "top" positioning as its neglected here because the parent Row aligns all its children center-vertically

    If we put a background color on each Box,

    Modifier.background(Color.LightGray/Gray/DarkGray)
    

    we can clearly see their equal width

    enter image description here