Skip to content

Instantly share code, notes, and snippets.

@cavin-macwan
Created March 17, 2025 18:40
Show Gist options
  • Save cavin-macwan/db9928ee9f7d747e7134e2d817829848 to your computer and use it in GitHub Desktop.
Save cavin-macwan/db9928ee9f7d747e7134e2d817829848 to your computer and use it in GitHub Desktop.
Type writer text effect in jetpack compose
@Composable
fun TypeWriterTextAnimation() {
Box(
modifier = Modifier
.fillMaxSize()
.padding(10.dp),
contentAlignment = Alignment.Center
) {
TypewriterText(
baseText = "Jetpack Compose is ",
parts = listOf("best", "super", "awesome")
)
}
}
@Composable
fun TypewriterText(
baseText: String,
parts: List<String>
) {
var partIndex by remember { mutableIntStateOf(0) }
var partText by remember { mutableStateOf("") }
val textToDisplay = "$baseText $partText"
LaunchedEffect(key1 = parts) {
while (partIndex <= parts.size) {
val part = parts[partIndex]
part.forEachIndexed { charIndex, _ ->
partText = part.substring(startIndex = 0, endIndex = charIndex + 1)
delay(100)
}
delay(1000)
part.forEachIndexed { charIndex, _ ->
partText = part
.substring(startIndex = 0, endIndex = part.length - (charIndex + 1))
delay(30)
}
delay(500)
partIndex = (partIndex + 1) % parts.size
}
}
Text(
text = textToDisplay,
style = TextStyle(
fontWeight = FontWeight.SemiBold,
fontSize = 40.sp,
letterSpacing = -(1.6).sp,
lineHeight = 52.sp
),
color = Color.Black,
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment