Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 536
Praveen P. Avatar asked Dec 30 '22 16:12

Praveen P.


1 Answers

Starting with M2 1.2.0-alpha02 you can use the Chip or FilterChip 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

With M3 (androidx.compose.material3) you can use one of these options:

  • AssistChip
  • FilterChip
  • InputChip
  • SuggestionChip

Something like:

AssistChip(
    onClick = { /* Do something! */ },
    label = { Text("Assist Chip") },
    leadingIcon = {
        Icon(
            Icons.Filled.Settings,
            contentDescription = "Localized description",
            Modifier.size(AssistChipDefaults.IconSize)
        )
    }
)

enter image description here

like image 119
Gabriele Mariotti Avatar answered Jan 08 '23 10:01

Gabriele Mariotti