Custom Rating Bar in Jetpack Compose (Tap and Drag)

Short answer: Jetpack Compose has no built-in rating bar, but you can build one in a few lines: draw a Row of star Icons, color the first n stars as selected and update the rating when the user taps or drags. This guide shows a simple tap version and a tap-and-drag version.

Simple rating bar (tap to rate)

@Composable
fun RatingBar(
    rating: Int,
    onRatingChange: (Int) -> Unit,
    modifier: Modifier = Modifier,
    maxStars: Int = 5,
    starSize: Dp = 32.dp,
    activeColor: Color = Color(0xFFFFC107),
    inactiveColor: Color = Color(0xFFBDBDBD)
) {
    Row(modifier = modifier) {
        for (index in 1..maxStars) {
            Icon(
                imageVector = Icons.Filled.Star,
                contentDescription = "Rate " + index + " of " + maxStars,
                tint = if (index <= rating) activeColor else inactiveColor,
                modifier = Modifier
                    .size(starSize)
                    .clickable { onRatingChange(index) }
            )
        }
    }
}

Use it with state hoisting. The parent owns the value:

@Composable
fun RatingDemo() {
    var rating by remember { mutableStateOf(3) }

    Column(Modifier.padding(16.dp)) {
        RatingBar(rating = rating, onRatingChange = { rating = it })
        Text("Your rating: " + rating + " / 5")
    }
}

Add import androidx.compose.runtime.getValue and setValue if by remember shows an error.

Rating bar with drag support

Many apps let users slide a finger across the stars. Handle taps and horizontal drags on the whole row and convert the touch position into a star count:

@Composable
fun DraggableRatingBar(
    rating: Int,
    onRatingChange: (Int) -> Unit,
    modifier: Modifier = Modifier,
    maxStars: Int = 5,
    starSize: Dp = 36.dp
) {
    fun ratingFromX(x: Float, width: Int): Int {
        val fraction = (x / width).coerceIn(0f, 1f)
        return ceil(fraction * maxStars).toInt().coerceIn(1, maxStars)
    }

    Row(
        modifier = modifier
            .width(starSize * maxStars)
            .pointerInput(maxStars) {
                detectTapGestures { offset ->
                    onRatingChange(ratingFromX(offset.x, size.width))
                }
            }
            .pointerInput(maxStars) {
                detectHorizontalDragGestures { change, _ ->
                    onRatingChange(ratingFromX(change.position.x, size.width))
                }
            }
            .semantics {
                contentDescription = "Rating " + rating + " out of " + maxStars
            }
    ) {
        for (index in 1..maxStars) {
            Icon(
                imageVector = Icons.Filled.Star,
                contentDescription = null,
                tint = if (index <= rating) Color(0xFFFFC107) else Color(0xFFBDBDBD),
                modifier = Modifier.size(starSize)
            )
        }
    }
}

Imports include androidx.compose.foundation.gestures.detectTapGestures, detectHorizontalDragGestures, androidx.compose.ui.input.pointer.pointerInput, androidx.compose.ui.semantics.semantics and kotlin.math.ceil.

Customize it

  • Colors: change activeColor and inactiveColor, or use theme colors such as MaterialTheme.colorScheme.primary.
  • Other shapes: swap Icons.Filled.Star for a heart or your own vector.
  • Read-only display: remove the click and gesture modifiers to show a rating without letting users change it.
  • Half stars: store the rating as a Float and draw a half-filled star icon with a clipped overlay or a drawable.

Accessibility

Give the control a clear contentDescription, as shown above, so screen readers announce the current value. For production apps, also consider adding custom accessibility actions to increase and decrease the rating.

Common mistakes

  • Keeping the rating state inside the component. Hoist it so the parent can save it.
  • Using too small a touch target. Keep each star at least 32-48 dp.
  • Forgetting the drag gesture on touch screens. Users often slide across the row.
  • Allowing a rating of zero by accident. Decide whether zero is valid and handle it.

FAQ

Is there a built-in RatingBar in Jetpack Compose?

No. Compose has no built-in rating bar, so you build one with icons as shown here, or use a library.

How do I make the rating bar read-only?

Remove the click and pointer input modifiers and pass a fixed rating.

How do I support half stars?

Use a Float rating and draw a half star for the fractional part, for example with Canvas or a clipped icon.

How do I save the rating?

Hold the rating in a ViewModel and write it to your database or API in onRatingChange. See our MVVM tutorial.

More: Jetpack Compose tutorials.