Jetpack Compose Auto-Scrolling List with a Start/Stop Toggle and Hex Color Switching
RobinhoodAndroid Engineer · Technical ScreenLast updated
Tests Jetpack Compose UI state management, coroutine lifecycle side-effects with LazyListState, and defensive hex color string parsing.
Auto-Scrolling List Screen in Jetpack Compose
In mobile financial applications, live data tickers and order books frequently scroll automatically or update visual themes on the fly. Your task is to implement a Jetpack Compose screen containing an auto-scrolling list of market tickers, along with controls to toggle scrolling and switch list item background colors parsed dynamically from hex color strings.
Task Requirements
-
Hex Color Parser Function: Implement a robust parser function
parseHexColor(hexString: String): Colorthat parses hex color strings into ComposeColorobjects.- Supports 6-character format (
#RRGGBBorRRGGBB, alpha defaults to0xFF). - Supports 8-character format (
#AARRGGBBorAARRGGBB). - If the input is invalid (malformed hex, wrong length, null/empty), it should return a safe fallback color (e.g.,
Color.Gray).
- Supports 6-character format (
-
Auto-Scrolling Behavior:
- Use a
LazyColumndriven by aLazyListState. - When auto-scrolling is enabled, the list should scroll smoothly downward continuously at a consistent pace (e.g., using
LazyListState.scrollByinside a coroutine loop). - When reaching the end of the list, it should either wrap around to the top or stop cleanly without crashing.
- When the user manually drags the list, auto-scrolling should gracefully handle interruption without causing coroutine leaks or frame drops.
- Use a
-
Screen Controls:
- Start/Stop Toggle: A button that toggles auto-scrolling on and off. The button label should reflect the current state (e.g., "Pause" vs. "Resume").
- Color Switcher: A button that cycles the item card background color through a predefined list of hex strings (e.g.,
listOf("#00C805", "#FF5000", "#1E2124", "#INVALID")).
Signatures
// Helper to parse hex string safely
fun parseHexColor(hexString: String): Color
// Main Composable Screen
@Composable
fun AutoScrollingListScreen(
items: List = (1..50).map { "Ticker Item #$it" },
colorHexList: List = listOf("#00C805", "#FF5000", "#1E2124", "#7B1FA2"),
modifier: Modifier = Modifier
)
Constraints
- Assume standard Jetpack Compose runtime and foundation libraries (
androidx.compose.foundation,androidx.compose.runtime). - Smooth scrolling should run on
Dispatchers.Mainwithout blocking the UI thread. - Changing the color state must not cause the entire list to reset its scroll position.
Hints
Hint 1
Use a `LaunchedEffect` keyed on the scrolling state flag to launch an infinite coroutine loop using `scrollBy()` with a small step and delay.
Hint 2
Strip optional '#' prefixes and use `hexString.toLongOrNull(16)` to safely parse 6- or 8-digit hexadecimal values without throwing uncaught exceptions.
Hint 3
Make sure user scroll gestures take priority; `scrollBy` can suspend or be cancelled if the user interacts with the list directly.
Follow-up questions
- How would you pause auto-scrolling automatically while the user is actively touching or dragging the list, and resume after a short timeout?
- How can you convert this list into an infinite looping marquee so that it continuously scrolls without ever hitting an end boundary?
- What performance considerations (such as derivedStateOf or remember) are critical when triggering state updates during continuous scroll animations?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.