{"record":{"id":"37c0066258e540b7","repo":"material-components/material-components-android","slug":"invalid-swipe-state","errorCode":null,"errorMessage":"Invalid swipe state: {}","messagePattern":"Invalid swipe state: (.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"lib/java/com/google/android/material/listitem/ListItemLayout.java","lineNumber":917,"sourceCode":"  public void setSwipeState(@StableSwipeState int swipeState, @RevealGravity int revealGravity) {\n    setSwipeState(swipeState, revealGravity, /* animate= */ true);\n  }\n\n  /**\n   * Sets the state to swipe the {@link SwipeableListItem} child to.\n   *\n   * @param swipeState The state to swipe to. This must be one of {@link\n   *     SwipeableListItem#STATE_CLOSED}, {@link SwipeableListItem#STATE_OPEN}, or {@link\n   *     SwipeableListItem#STATE_SWIPE_PRIMARY_ACTION}\n   * @param revealGravity The gravity of the {@link RevealableListItem} to reveal when swiping.\n   * @param animate Whether to animate to the given swipe state.\n   */\n  public void setSwipeState(\n      @StableSwipeState int swipeState, @RevealGravity int revealGravity, boolean animate) {\n    if (swipeState != STATE_CLOSED\n        && swipeState != STATE_OPEN\n        && swipeState != STATE_SWIPE_PRIMARY_ACTION) {\n      throw new IllegalArgumentException(\"Invalid swipe state: \" + swipeState);\n    } else if (!(contentView instanceof SwipeableListItem) || !swipeToRevealLayoutExists()) {\n      throw new IllegalArgumentException(\n          \"ListItemLayout must have a SwipeableListItem child and a RevealableListItem child to be\"\n              + \" swiped.\");\n    } else if (swipeState != STATE_CLOSED && !swipeToRevealLayoutExistsForGravity(revealGravity)) {\n      throw new IllegalArgumentException(\n          \"No RevealableListItem is defined for the given gravity: \" + revealGravity);\n    }\n\n    Runnable runnable =\n        () -> {\n          if (!animate) {\n            if (viewDragHelper != null) {\n              viewDragHelper.abort();\n            }\n            int finalLeft = getOffsetForSwipeState(swipeState, revealGravity);\n            contentView.offsetLeftAndRight(finalLeft - contentView.getLeft());\n            updateSwipeProgress(finalLeft);","sourceCodeStart":899,"sourceCodeEnd":935,"githubUrl":"https://github.com/material-components/material-components-android/blob/ac7e18efeefb331850c561faf9ab8bf81d27ba68/lib/java/com/google/android/material/listitem/ListItemLayout.java#L899-L935","documentation":"The gravity-based setSwipeState only accepts the three stable swipe states (STATE_CLOSED, STATE_OPEN, STATE_SWIPE_PRIMARY_ACTION). The @StableSwipeState annotation documents this at compile time; at runtime an unrecognized value throws IllegalArgumentException with the value printed, before any layout work starts.","triggerScenarios":"Passing an arbitrary/unknown int as swipeState (e.g. from a bundled intent extra or persisted preference without validation); using constants copied from a different swipe library; typos in mirrored constants after a library update.","commonSituations":"Restoring swipe state from savedInstanceState or a database; receiving desired state from deep links; code shared across app modules with divergent constant definitions.","solutions":["Whitelist incoming values: validate the stored state is one of the three constants before calling setSwipeState, defaulting to STATE_CLOSED.","Always reference SwipeableListItem.STATE_* constants instead of literal ints.","Annotate your own parameters with @StableSwipeState so IDE lint flags invalid values."],"exampleFix":"// before\nlistItemLayout.setSwipeState(intentExtraState, gravity, true); // unvalidated\n\n// after\nint state = (extra == SwipeableListItem.STATE_OPEN\n        || extra == SwipeableListItem.STATE_SWIPE_PRIMARY_ACTION)\n    ? extra : SwipeableListItem.STATE_CLOSED;\nlistItemLayout.setSwipeState(state, gravity, true);","handlingStrategy":"validation","validationCode":"int safe = (state == SwipeableListItem.STATE_OPEN\n    || state == SwipeableListItem.STATE_SWIPE_PRIMARY_ACTION)\n        ? state : SwipeableListItem.STATE_CLOSED;\nlistItemLayout.setSwipeState(safe, revealGravity, animate);","typeGuard":"static boolean isStableSwipeState(int s) {\n  return s == SwipeableListItem.STATE_CLOSED\n      || s == SwipeableListItem.STATE_OPEN\n      || s == SwipeableListItem.STATE_SWIPE_PRIMARY_ACTION;\n}","tryCatchPattern":null,"preventionTips":["Whitelist persisted/intent-supplied states against the three constants.","Keep a single source of truth for swipe-state constants in your app.","Add unit tests covering unknown-state fallback behavior."],"tags":["material-components","listitem","swipe","validation"],"backgroundTag":null,"analyzedSha":"ac7e18efeefb331850c561faf9ab8bf81d27ba68","analyzedAt":"2026-08-14T15:35:35.735Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}