Search code examples
androidkotlinprogressandroid-jetpack-compose

Android Jetpack Compose CircularProgressIndicator Set Min and Max Progress


I'm trying to set min and max progress of CircularProgressIndicator But I couldn't found any solution.

This is the given Composable CircularProgressIndicator:

@Composable
fun CircularProgressIndicator(
    /*@FloatRange(from = 0.0, to = 1.0)*/
    progress: Float,
    modifier: Modifier = Modifier,
    color: Color = MaterialTheme.colors.primary,
    strokeWidth: Dp = ProgressIndicatorDefaults.StrokeWidth
) {
    val stroke = with(LocalDensity.current) {
        Stroke(width = strokeWidth.toPx(), cap = StrokeCap.Butt)
    }
    Canvas(
        modifier
            .progressSemantics(progress)
            .size(CircularIndicatorDiameter)
            .focusable()
    ) {
        // Start at 12 O'clock
        val startAngle = 270f
        val sweep = progress * 360f
        drawDeterminateCircularIndicator(startAngle, sweep, color, stroke)
    }
}

It only accepts progress in the range of float which 0.0 to 1.0 I want to change this to accept value from 0 to 100.

Note:

I also found out this Modifier.progressSemantics But I don't know how to use it to change the progress range.

@Stable
fun Modifier.progressSemantics(
    value: Float,
    valueRange: ClosedFloatingPointRange<Float> = 0f..1f,
    /*@IntRange(from = 0)*/
    steps: Int = 0
): Modifier {
    val progress = (
        if (valueRange.endInclusive - valueRange.start == 0f) 0f
        else (value - valueRange.start) / (valueRange.endInclusive - valueRange.start)
        ).coerceIn(0f, 1f)

    // We only display 0% or 100% when it is exactly 0% or 100%.
    val percent = when (progress) {
        0f -> 0
        1f -> 100
        else -> (progress * 100).roundToInt().coerceIn(1, 99)
    }

    return semantics {
        stateDescription = "$percent percent"
        progressBarRangeInfo =
            ProgressBarRangeInfo(value.coerceIn(valueRange), valueRange, steps)
    }
}

Any help is appreciated.


Solution

  • In your case you don't need to change the progress range.
    Just turn your range into 0.0 - 1.0.

    Just divide the progress value by 60000.
    1000/60000 = 0.016 for each tick.

    If you want to reverse the count just use something like:
    val progress = 1 - (givenValueOnTick.toFloat / 60000)