Blankj/AndroidUtilCode · 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

RecycleViewDivider.setOrientation throws IllegalArgumentException unless the orientation is exactly HORIZONTAL (LinearLayout.HORIZONTAL==0) or VERTICAL (LinearLayout.VERTICAL==1). The divider must know its drawing axis, so any other int is rejected at construction.

Source

Thrown at lib/base/src/main/java/com/blankj/base/rv/RecycleViewDivider.java:56

    }

    public RecycleViewDivider(Context context, int orientation, @NonNull Drawable divider) {
        this(context, orientation, divider, false);
    }

    public RecycleViewDivider(Context context, int orientation, @DrawableRes int resId, boolean showFooterDivider) {
        this(context, orientation, ContextCompat.getDrawable(context, resId), showFooterDivider);
    }

    public RecycleViewDivider(Context context, int orientation, @NonNull Drawable divider, boolean showFooterDivider) {
        setOrientation(orientation);
        mDivider = divider;
        mShowFooterDivider = showFooterDivider;
    }

    private void setOrientation(int orientation) {
        if (orientation != HORIZONTAL && orientation != VERTICAL) {
            throw new IllegalArgumentException(
                    "Invalid orientation. It should be either HORIZONTAL or VERTICAL");
        }
        mOrientation = orientation;
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        if (parent.getLayoutManager() == null) {
            return;
        }
        if (mOrientation == VERTICAL) {
            drawVertical(c, parent);
        } else {
            drawHorizontal(c, parent);
        }
    }

    @SuppressLint("NewApi")

View on GitHub (pinned to 7b4caf9e54)

Solutions

  1. Pass one of the RecycleViewDivider.HORIZONTAL or RecycleViewDivider.VERTICAL constants explicitly.
  2. If the orientation comes from a variable, validate it is 0 or 1 (or one of the two constants) before constructing the divider.
  3. Use the matching constant names (RecycleViewDivider.HORIZONTAL/VERTICAL) to avoid confusing them with other libraries' constants.

Example fix

// before
new RecycleViewDivider(ctx, layoutManager.getOrientation(), R.drawable.div);
// if getOrientation returns an unexpected value -> throws

// after
int o = layoutManager.getOrientation();
new RecycleViewDivider(ctx,
    o == RecyclerView.VERTICAL ? RecycleViewDivider.VERTICAL : RecycleViewDivider.HORIZONTAL,
    R.drawable.div);
Defensive patterns

Strategy: validation

Validate before calling

int o = (orientation == LinearLayout.VERTICAL)
    ? RecycleViewDivider.VERTICAL
    : RecycleViewDivider.HORIZONTAL;
new RecycleViewDivider(context, o, divider);

Type guard

private static boolean isValidDividerOrientation(int o) {
    return o == RecycleViewDivider.HORIZONTAL || o == RecycleViewDivider.VERTICAL;
}

Prevention

When it happens

Trigger: Passing an arbitrary int, a wrong constant, or a recycled/initialised-to-default (-1 or 0 interpreted wrong) value as the orientation argument to any RecycleViewDivider constructor.

Common situations: Passing RecyclerView.HORIZONTAL/VERTICAL by mistake instead of RecycleViewDivider.HORIZONTAL/VERTICAL (values happen to match but reads wrong); passing a LayoutManager orientation constant stored in a variable that defaulted to 0/other; passing a configuration value read from resources as a raw int.

Related errors


AI-assisted analysis of Blankj/AndroidUtilCode@7b4caf9e54 (2026-08-14). Data as JSON: /api/errors/b9fdfefcdcdcdba8. Report an issue: GitHub.