material-components/material-components-android · error · IllegalArgumentException

Slide distance must be positive. If attempting to reverse th

Error message

Slide distance must be positive. If attempting to reverse the direction of the slide, use setSlideEdge(int) instead.

What it means

SlideDistanceProvider.setSlideDistance (SlideDistanceProvider.java:95) validates that the pixel distance used to slide a view during a MaterialSharedAxis transition is non-negative. A negative distance would reverse the slide direction, but direction is expressed separately via the slide edge; to keep the two concerns disjoint, negative distances are rejected with IllegalArgumentException (note: the Javadoc says negative throws, the check is slideDistance < 0, so 0 is accepted).

Source

Thrown at lib/java/com/google/android/material/transition/SlideDistanceProvider.java:95

   * @see #setSlideDistance(int)
   */
  @Px
  public int getSlideDistance() {
    return slideDistance;
  }

  /**
   * Set the distance this animator will translate its target.
   *
   * <p>By default, this value is set to -1 which indicates that the default slide distance,
   * R.dimen.mtrl_transition_shared_axis_slide_distance will be used. Setting the slide distance to
   * any other value will override this default.
   *
   * @throws IllegalArgumentException If {@code slideDistance} is negative.
   */
  public void setSlideDistance(@Px int slideDistance) {
    if (slideDistance < 0) {
      throw new IllegalArgumentException(
          "Slide distance must be positive. If attempting to reverse the direction of the slide,"
              + " use setSlideEdge(int) instead.");
    }
    this.slideDistance = slideDistance;
  }

  @Nullable
  @Override
  public Animator createAppear(@NonNull ViewGroup sceneRoot, @NonNull View view) {
    return createTranslationAppearAnimator(
        sceneRoot, view, slideEdge, getSlideDistanceOrDefault(view.getContext()));
  }

  @Nullable
  @Override
  public Animator createDisappear(@NonNull ViewGroup sceneRoot, @NonNull View view) {
    return createTranslationDisappearAnimator(
        sceneRoot, view, slideEdge, getSlideDistanceOrDefault(view.getContext()));

View on GitHub (pinned to ac7e18efee)

Solutions

  1. To reverse direction, change the edge/side the animation comes from (setSlideEdge or the forward boolean passed to MaterialSharedAxis), keeping distance positive.
  2. If distance is computed, wrap with Math.abs() once you have verified the intended magnitude.
  3. Never pass -1 as "use default" — omit setSlideDistance entirely to keep the default R.dimen.mtrl_transition_shared_axis_slide_distance.

Example fix

// before
provider.setSlideDistance(-distancePx); // attempt to reverse

// after
provider.setSlideDistance(distancePx);
// reverse by constructing with forward=false or a different slide edge:
new MaterialSharedAxis(MaterialSharedAxis.X, /* forward= */ false);
Defensive patterns

Strategy: validation

Validate before calling

static int sanitizeSlideDistance(int px) {
  if (px < 0) throw new IllegalArgumentException("slideDistance must be >= 0; got " + px);
  return px;
}
provider.setSlideDistance(sanitizeSlideDistance(computedPx));
// or simply: provider.setSlideDistance(Math.abs(computedPx)) when only magnitude matters

Prevention

When it happens

Trigger: Calling provider.setSlideDistance(-120) intending to reverse motion; computing distance from a dimension resource that resolves negative; passing -1 as a sentinel/"unset" value (the default sentinel is -1 internally but is not a legal user value).

Common situations: Developers trying to flip slide direction with a negative number instead of setSlideEdge or swapping forward flag; using dimension math that can go negative in RTL or scaled densities; porting code from other libraries where negative distances are allowed.

Related errors


AI-assisted analysis of material-components/material-components-android@ac7e18efee (2026-08-14). Data as JSON: /api/errors/15319fc173ed1e5b. Report an issue: GitHub.