How to create a circular Outlined button with jetpack compose

27.3k Views Asked by At

I am trying to create a circular OutlinedButton with an icon in the center without text.

    OutlinedButton(onClick = { /*TODO*/ },
        shape = CircleShape,
        border= BorderStroke(1.dp, Color.Blue)
    ) {
        Icon(Icons.Default.Add, contentDescription = "content description")
    }

The final button has an oval shape:

enter image description here

Using an IconButton:

    IconButton(onClick = {  },
        modifier = Modifier
            .clip(CircleShape)
            .border(1.dp, Color.Red)
    ) {
        Icon(Icons.Default.Add, contentDescription = "content description",tint = Color.Red)
    }

This is the final result:

enter image description here

2

There are 2 best solutions below

1
On BEST ANSWER

You can use the OutlinedButton removing the contentPadding.
Something like:

    OutlinedButton(onClick = { /*TODO*/ },
        modifier= Modifier.size(50.dp),  //avoid the oval shape
        shape = CircleShape,
        border= BorderStroke(1.dp, Color.Blue),
        contentPadding = PaddingValues(0.dp),  //avoid the little icon
        colors = ButtonDefaults.outlinedButtonColors(contentColor =  Color.Blue)
    ) {
         Icon(Icons.Default.Add, contentDescription = "content description")
    }

enter image description here

or the IconButton removing the clip modifier and using the shape inside the border parameter:

    IconButton(onClick = {  },
          modifier = Modifier
              .then(Modifier.size(50.dp))
              .border(1.dp, Color.Red, shape = CircleShape)
              ) {
        Icon(Icons.Default.Add, contentDescription = "content description", tint = Color.Red)
    }

enter image description here

0
On

There is not an outlined icon button component in the M2 package (androidx.compose.material), but in M3 (androidx.compose.material3), since v1.0.0-alpha10 released April 20, 2022, there is an OutlinedIconButton, you can use it as follows:

import androidx.compose.material3.Icon
import androidx.compose.material3.OutlinedIconButton

OutlinedIconButton(onClick = { /* doSomething() */ }) {
    Icon(Icons.Outlined.Lock, contentDescription = "Localized description")
}

Similarly there are FilledIconButton and FilledTonalIconButton for the other M3 styles.