Katkat

Jetpack Compose Auto-Scrolling List with a Start/Stop Toggle and Hex Color Switching

RobinhoodAndroid Engineer · Technical Screen

Last 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

  1. Hex Color Parser Function: Implement a robust parser function parseHexColor(hexString: String): Color that parses hex color strings into Compose Color objects.

    • Supports 6-character format (#RRGGBB or RRGGBB, alpha defaults to 0xFF).
    • Supports 8-character format (#AARRGGBB or AARRGGBB).
    • If the input is invalid (malformed hex, wrong length, null/empty), it should return a safe fallback color (e.g., Color.Gray).
  2. Auto-Scrolling Behavior:

    • Use a LazyColumn driven by a LazyListState.
    • When auto-scrolling is enabled, the list should scroll smoothly downward continuously at a consistent pace (e.g., using LazyListState.scrollBy inside 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.
  3. 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.Main without 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?