Skip to content

Instantly share code, notes, and snippets.

@Wajahat-Jawaid
Created November 2, 2022 13:31
Show Gist options
  • Save Wajahat-Jawaid/4d5ab04c09f87af4358adcc3dd59edf3 to your computer and use it in GitHub Desktop.
Save Wajahat-Jawaid/4d5ab04c09f87af4358adcc3dd59edf3 to your computer and use it in GitHub Desktop.
@Composable
fun StarButton(
isFavorite: Boolean,
onClick: () -> Unit,
modifier: Modifier = Modifier
) {
val clickLabel = stringResource(if (isFavorite) R.string.unstar else R.string.star)
IconToggleButton(
checked = isFavorite,
onCheckedChange = {
onClick()
},
modifier = modifier.semantics {
// Use a custom click label that accessibility services can communicate to the user.
// We only want to override the label, not the actual action, so for the action we pass null.
this.onClick(label = clickLabel, action = null)
}
) {
StarIcon(isFavorite)
}
}
@Composable
fun StarIcon(
isFavorite: Boolean
) {
// If the file is starred, show filled icon, else show the bordered icon
Icon(
painter =
if (isFavorite) painterResource(id = R.drawable.ic_star_filled)
else painterResource(id = R.drawable.ic_star_border),
tint = Color(246, 190, 0),
contentDescription = stringResource(R.string.starred)
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment