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

Invalid orientation: {}. It should be either HORIZONTAL or V

Error message

Invalid orientation: {}. It should be either HORIZONTAL or VERTICAL

What it means

MaterialDividerItemDecoration.setOrientation(int) accepts only the constants HORIZONTAL (vertical divider line) or VERTICAL (horizontal divider line), matching the orientation of the RecyclerView's LayoutManager. Any other int — including the LayoutManager's own constants or an uninitialized field — throws IllegalArgumentException to prevent silently drawing dividers in the wrong direction.

Source

Thrown at lib/java/com/google/android/material/divider/MaterialDividerItemDecoration.java:126

    dividerDrawable = new ShapeDrawable();
    setDividerColor(color);
    setOrientation(orientation);
  }

  /**
   * Sets the orientation for this divider. This should be called if {@link
   * RecyclerView.LayoutManager} changes orientation.
   *
   * <p>A {@link #HORIZONTAL} orientation will draw a vertical divider, and a {@link #VERTICAL}
   * orientation a horizontal divider.
   *
   * @param orientation The orientation of the {@link RecyclerView} this divider is associated with:
   *     {@link #HORIZONTAL} or {@link #VERTICAL}
   */
  public void setOrientation(int orientation) {
    if (orientation != HORIZONTAL && orientation != VERTICAL) {
      throw new IllegalArgumentException(
          "Invalid orientation: " + orientation + ". It should be either HORIZONTAL or VERTICAL");
    }
    this.orientation = orientation;
  }

  public int getOrientation() {
    return orientation;
  }

  /**
   * Sets the thickness of the divider.
   *
   * @param thickness The thickness value to be set.
   * @see #getDividerThickness()
   * @attr ref com.google.android.material.R.styleable#MaterialDivider_dividerThickness
   */
  public void setDividerThickness(@Px int thickness) {
    this.thickness = thickness;

View on GitHub (pinned to ac7e18efee)

Solutions

  1. Pass only MaterialDividerItemDecoration.HORIZONTAL or MaterialDividerItemDecoration.VERTICAL — use the LayoutManager orientation: decoration.setOrientation(layoutManager.getOrientation());
  2. Default any persisted/config-sourced orientation to a valid constant before calling setOrientation (e.g.VERTICAL if value is neither).
  3. Remove hardcoded 0/1 literals and reference the class constants so refactors stay safe.

Example fix

// before
dividerItemDecoration.setOrientation(orientationFromPrefs); // may be -1/0 unexpectedly

// after
int orientation = (orientationFromPrefs == MaterialDividerItemDecoration.HORIZONTAL)
    ? MaterialDividerItemDecoration.HORIZONTAL
    : MaterialDividerItemDecoration.VERTICAL;
dividerItemDecoration.setOrientation(orientation);
Defensive patterns

Strategy: validation

Validate before calling

int orientation = (value == MaterialDividerItemDecoration.HORIZONTAL
    || value == MaterialDividerItemDecoration.VERTICAL)
        ? value : MaterialDividerItemDecoration.VERTICAL;
divider.setOrientation(orientation);

Try / catch

try { divider.setOrientation(raw); } catch (IllegalArgumentException e) { divider.setOrientation(LinearLayoutManager.getOrientation()); }

Prevention

When it happens

Trigger: Calling setOrientation(0) / setOrientation(1) with hardcoded values that map to the wrong constants; passing LinearLayoutManager.HORIZONTAL to the decoration but mixing up constant sources; calling setOrientation with a value read from a config/bundle that can be absent (default 0 or -1); reacting to onConfigurationChanged with a computed orientation that is neither constant.

Common situations: Copy-pasting sample code that uses raw ints; switching between divider classes (DividerItemDecoration vs MaterialDividerItemDecoration) whose constant values differ; handling layout direction changes yourself instead of letting the decoration read it.

Related errors


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