3

I want to show timer of 1 minute when user click on Re-send

enter image description here

enter image description here

Text(
   text = "Re-send",
   modifier = Modifier.clickable { },
   color = Color.Blue
)
Unknown
  • 187
  • 7

2 Answers2

6

For creating text with 2 colors you need annotatedString, for re-send to be clickable you need index of re-send and ClickableText

to create a timer you can use LaunchedEffect as in snippet below

@Composable
private fun ResendTextSample() {

    val str = "Did you not receive the email? "
    val length = str.length

    var isTimerActive by remember {
        mutableStateOf(false)
    }

    var time by remember { mutableStateOf("") }

    LaunchedEffect(key1 = isTimerActive) {
       if(isTimerActive){
           var second = 0
           while (second < 15) {
               time = if(second <10) "0:0$second" else "0:$second"
               delay(1000)
               second++
           }
           isTimerActive = false
       }
    }

    val annotatedLinkString = buildAnnotatedString {

        append(str)
        withStyle(
            SpanStyle(
                color = Color.Blue,
            )
        ) {
            if (!isTimerActive) {
                append("Re-send")
            } else {
                append(time)
            }
        }
        append("\nCheck your spam filter")

    }

    ClickableText(
        text = annotatedLinkString,
        style = TextStyle(fontSize = 20.sp),
        onClick = {
            if (!isTimerActive && it >= length && it <= length + 7) {
                isTimerActive = true
            }
        }
    )
}

Result. In demo i set max time to 15 seconds to show that resend is enabled.

enter image description here

Thracian
  • 43,021
  • 16
  • 133
  • 222
  • 1
    Thanks for answering and giving more information, I was putting each text into a different `TEXT Compose` – Unknown Nov 30 '22 at 20:25
  • 1
    You can accomplish it the way you want. Updated answer and added if check to not run timer inside `LaunchedEffect` while timer is not active – Thracian Dec 01 '22 at 08:25
-1

To create a timer you can use LaunchedEffect when timer is not active.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 04 '22 at 23:23