DrKLO/Telegram · error · IllegalArgumentException

Invalid orientation. It should be either HORIZONTAL or VERTI

Error message

Invalid orientation. It should be either HORIZONTAL or VERTICAL

What it means

DividerItemDecoration draws a divider line/drawable between items and must know whether the list scrolls horizontally or vertically so it can position the drawable on the correct axis. setOrientation accepts only HORIZONTAL (0) or VERTICAL (1) from LinearLayoutManager. Any other int (e.g. -1, 2, or an unrelated constant) is meaningless for divider placement and is rejected immediately rather than silently rendering nothing.

Source

Thrown at TMessagesProj/src/main/java/androidx/recyclerview/widget/DividerItemDecoration.java:85

        final TypedArray a = context.obtainStyledAttributes(ATTRS);
        mDivider = a.getDrawable(0);
        if (mDivider == null) {
            Log.w(TAG, "@android:attr/listDivider was not set in the theme used for this "
                    + "DividerItemDecoration. Please set that attribute all call setDrawable()");
        }
        a.recycle();
        setOrientation(orientation);
    }

    /**
     * Sets the orientation for this divider. This should be called if
     * {@link RecyclerView.LayoutManager} changes orientation.
     *
     * @param orientation {@link #HORIZONTAL} or {@link #VERTICAL}
     */
    public void setOrientation(int orientation) {
        if (orientation != HORIZONTAL && orientation != VERTICAL) {
            throw new IllegalArgumentException(
                    "Invalid orientation. It should be either HORIZONTAL or VERTICAL");
        }
        mOrientation = orientation;
    }

    /**
     * Sets the {@link Drawable} for this divider.
     *
     * @param drawable Drawable that should be used as a divider.
     */
    public void setDrawable(@NonNull Drawable drawable) {
        if (drawable == null) {
            throw new IllegalArgumentException("Drawable cannot be null.");
        }
        mDivider = drawable;
    }

    /**

View on GitHub (pinned to 45ab8f4308)

Solutions

  1. Derive orientation from the LayoutManager: dividerItemDecoration.setOrientation(((LinearLayoutManager) layoutManager).getOrientation());
  2. After calling layoutManager.setOrientation(...), also update the divider.
  3. Validate the int is 0 or 1 before calling setOrientation if it comes from external input.

Example fix

// before
DividerItemDecoration did = new DividerItemDecoration(this, 5); // throws

// after
int orientation = ((LinearLayoutManager) layoutManager).getOrientation();
DividerItemDecoration did = new DividerItemDecoration(this, orientation);
Defensive patterns

Strategy: validation

Validate before calling

// Derive orientation from the actual LayoutManager
int safeOrientation(RecyclerView.LayoutManager lm) {
    if (!(lm instanceof LinearLayoutManager)) return DividerItemDecoration.VERTICAL;
    return ((LinearLayoutManager) lm).getOrientation();
}

Prevention

When it happens

Trigger: Passing layoutManager.canScrollVertically() ? 1 : 0 incorrectly; passing RecyclerView.HORIZONTAL/VERTICAL when out of sync with the actual LayoutManager orientation; passing a raw int from a config/JSON without validation; calling setOrientation with a value computed from a null LayoutManager.

Common situations: Hardcoding orientation then later switching the LayoutManager; reading orientation from a preferences file that can be corrupt; copy-pasting a DividerItemDecoration setup that referenced a different constant set.

Related errors


AI-assisted analysis of DrKLO/Telegram@45ab8f4308 (2026-08-14). Data as JSON: /api/errors/0b3d3b3151376ac9. Report an issue: GitHub.