jetpack compose - CircularProgressIndicator example

MainActivity.kt

package com.cfsuman.jetpackcompose

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.rotate
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {
            MainContent()
        }
    }


    @Composable
    fun MainContent(){
        val isEnable = remember { mutableStateOf(true) }
        val result = remember { mutableStateOf("") }
        val progress = remember { mutableStateOf(0.0F)}

        Column(
            modifier = Modifier
                .padding(16.dp)
                .fillMaxWidth(),
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.spacedBy(16.dp)
        ) {
            Button(
                onClick = {
                    progress.value = 0.0F
                    isEnable.value = false
                    result.value = "Task running...."

                    Thread {
                        while (progress.value < 1){
                            Thread.sleep(1 * 100)
                            progress.value = progress.value + 0.01F

                            if (progress.value >= 1F){
                                isEnable.value = true
                            }
                            val finished = (progress.value * 100).toInt()
                            result.value = "Done $finished of 100 tasks"
                        }
                    }.start()
                },
                enabled = isEnable.value
            ) {
                Text(text = "Run Tasks")
            }

            CircularProgressIndicator(
                progress = progress.value,
                Modifier
                    .rotate(-90F)
                    .size(200.dp),
                color = Color(0xFF7BB661),
                strokeWidth = 12.dp
            )

            Text(
                text = result.value,
                fontSize = 20.sp
            )
        }
    }


    @Preview
    @Composable
    fun ComposablePreview(){
        //MainContent()
    }
}
More android jetpack compose tutorials