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

AppBarLayout is always vertical and does not support horizon

Error message

AppBarLayout is always vertical and does not support horizontal orientation

What it means

AppBarLayout extends LinearLayout but overrides setOrientation to lock the orientation to VERTICAL, because its scrolling behavior and layout logic only support vertical children. Any attempt to set HORIZONTAL (or any non-vertical value) throws IllegalArgumentException immediately.

Source

Thrown at lib/java/com/google/android/material/appbar/AppBarLayout.java:744

            ? null
            : behavior.saveScrollState(AbsSavedState.EMPTY_STATE, this);
    // Invalidate the scroll ranges
    totalScrollRange = INVALID_SCROLL_RANGE;
    downPreScrollRange = INVALID_SCROLL_RANGE;
    downScrollRange = INVALID_SCROLL_RANGE;
    // Restores the previous scrolling state. Don't override if there's a previously saved state
    // which has not be restored yet. Multiple re-measuring can happen before the scroll state
    // is actually restored. We don't want to restore the state in-between those re-measuring,
    // since they can be incorrect.
    if (savedState != null) {
      behavior.restoreScrollState(savedState, false);
    }
  }

  @Override
  public void setOrientation(int orientation) {
    if (orientation != VERTICAL) {
      throw new IllegalArgumentException(
          "AppBarLayout is always vertical and does not support horizontal orientation");
    }
    super.setOrientation(orientation);
  }

  @Override
  protected void onAttachedToWindow() {
    super.onAttachedToWindow();

    MaterialShapeUtils.setParentAbsoluteElevation(this);
  }

  @Override
  @NonNull
  public CoordinatorLayout.Behavior<AppBarLayout> getBehavior() {
    behavior = new AppBarLayout.Behavior();
    return behavior;
  }

View on GitHub (pinned to ac7e18efee)

Solutions

  1. Remove the setOrientation call or use setOrientation(LinearLayout.VERTICAL).
  2. Remove android:orientation="horizontal" from the AppBarLayout XML tag (vertical is the default).
  3. If you truly need horizontal arrangement, use a LinearLayout or other horizontal container inside or instead of AppBarLayout.

Example fix

<!-- before -->
<com.google.android.material.appbar.AppBarLayout
    android:orientation="horizontal"
    ... />

<!-- after -->
<com.google.android.material.appbar.AppBarLayout ... />
<!-- default orientation is vertical; arrange children in a horizontal LinearLayout if needed -->
Defensive patterns

Strategy: validation

Validate before calling

int orientation = LinearLayout.VERTICAL; // AppBarLayout only accepts vertical
if (orientation == LinearLayout.VERTICAL) {
  appBarLayout.setOrientation(orientation);
}

Prevention

When it happens

Trigger: Calling appBarLayout.setOrientation(LinearLayout.HORIZONTAL); setting android:orientation="horizontal" in XML on an AppBarLayout; or programmatically applying a generic orientation constant that is not VERTICAL.

Common situations: Treating AppBarLayout as a normal LinearLayout; copying a horizontal LinearLayout layout block and changing the tag to AppBarLayout; code that calls setOrientation based on a configuration flag that evaluates to HORIZONTAL.

Related errors


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