'Create chip with outline Jetpack Compose

I have the following composable function to build a Chip:

@Composable
fun CategoryChip(
  category: String,
  isSelected: Boolean = false,
  onSelectedCategoryChanged: (String) -> Unit,
  onExecuteSearch: () -> Unit
) {
  Surface(
    modifier = Modifier.padding(end = 8.dp, bottom = 8.dp),
    elevation = 8.dp,
    shape = RoundedCornerShape(16.dp),
    color = when {
      isSelected -> colorResource(R.color.teal_200)
      else -> colorResource(R.color.purple_500)
    }
  ) {
    Row(modifier = Modifier
      .toggleable(
        value = isSelected,
        onValueChange = {
          onSelectedCategoryChanged(category)
          onExecuteSearch()
        }
      )) {
      Text(
        text = category,
        style = MaterialTheme.typography.body2,
        color = Color.White,
        modifier = Modifier.padding(8.dp)
      )
    }
  }
}

This creates the following chip:

enter image description here

But what I am trying to achieve is the following:

enter image description here

Is it possible to create a shape like that with Jetpack Compose?



Solution 1:[1]

Yes, all you have to do is add a border to your surface.

Surface(
    modifier = Modifier.padding(end = 8.dp, bottom = 8.dp),
    elevation = 8.dp,
    shape = RoundedCornerShape(16.dp),
    border = BorderStroke(
        width = 1.dp,
        color = when {
            isSelected -> colorResource(R.color.teal_200)
            else -> colorResource(R.color.purple_500)
        }
     )
)

Solution 2:[2]

Starting with 1.2.0-alpha02 you can use the Chip composable:

Chip(
    onClick = { /* Do something! */ },
    border = BorderStroke(
         ChipDefaults.OutlinedBorderSize,
         Color.Red
    ),
    colors = ChipDefaults.chipColors(
        backgroundColor = Color.White,
        contentColor = Color.Red
    ),
    leadingIcon = {
        Icon(
            Icons.Filled.Settings,
            contentDescription = "Localized description"
        )
    }
) {
    Text("Change settings")
}

enter image description here

Solution 3:[3]

Building on Code Poet's answer i wanted to show how to do a Material Chip with background color:

@Composable
fun buildChip(label: String, icon: ImageVector? = null) {
    Box(modifier = Modifier.padding(8.dp)) {
        Surface(
            elevation = 1.dp,
            shape = MaterialTheme.shapes.small,
            color = Color.LightGray
        ) {
            Row(verticalAlignment = Alignment.CenterVertically) {
                if (icon != null) Icon(
                    icon,
                    modifier = Modifier
                        .fillMaxHeight()
                        .padding(horizontal = 4.dp)
                )
                Text(
                    label,
                    modifier = Modifier.padding(8.dp),
                    style = MaterialTheme.typography.button.copy(color = Color.DarkGray)
                )
            }
        }
    }
}

Solution 4:[4]

Simply use ChipDefaults.outlinedBorder and Defaults.outlinedChipColors():

Chip(
    onClick = {},
    border = ChipDefaults.outlinedBorder,
    colors = ChipDefaults.outlinedChipColors(),
) {
    Text(
        text = "Trends",
    )
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Code Poet
Solution 2 Gabriele Mariotti
Solution 3 Rody Davis
Solution 4 Roberto Leinardi