umano/AndroidSlidingUpPanel · error · java.lang.IllegalArgumentException

gravity must be set to either top or bottom

Error message

gravity must be set to either top or bottom

What it means

SlidingUpPanelLayout supports only TOP or BOTTOM gravity; setGravity() validates the argument and throws IllegalArgumentException for anything else. The gravity determines whether the panel slides up from the bottom or down from the top.

Solutions

  1. Pass only Gravity.TOP or Gravity.BOTTOM to setGravity()
  2. Check the value being forwarded; it likely comes from another widget's gravity attribute
  3. Instead of programmatic setGravity, use the XML attribute umanoScrollableView/panel-gravity style attr supported by the layout

Example fix

// before
layout.setGravity(Gravity.CENTER);
// after
layout.setGravity(Gravity.BOTTOM); // or Gravity.TOP
Defensive patterns

Strategy: validation

Validate before calling

int g = resolveGravity(someGravity);
if (g != Gravity.TOP && g != Gravity.BOTTOM) g = Gravity.BOTTOM;
layout.setGravity(g);

Type guard

boolean isValidPanelGravity(int g) { return g == Gravity.TOP || g == Gravity.BOTTOM; }

Try / catch

try { layout.setGravity(g); } catch (IllegalArgumentException e) { layout.setGravity(Gravity.BOTTOM); }

Prevention

When it happens

Trigger: Calling setGravity() with a gravity value other than Gravity.TOP or Gravity.BOTTOM (e.g. Gravity.LEFT, Gravity.RIGHT, Gravity.CENTER, 0, or a combined flag).

Common situations: Developers reuse a gravity variable intended for a different widget (LinearLayout, WindowManager flags) which allows LEFT/RIGHT/CENTER, then pass it to this layout; or pass 0/unset constants from configuration.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of umano/AndroidSlidingUpPanel@45a460435b (2026-09-11). Data as JSON: /api/errors/03d9b732b64cf6e0. Report an issue: GitHub.

Appendix: source

Thrown at library/src/main/java/com/sothree/slidinguppanel/SlidingUpPanelLayout.java:366

    }

    /**
     * Set the Drag View after the view is inflated
     */
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        if (mDragViewResId != -1) {
            setDragView(findViewById(mDragViewResId));
        }
        if (mScrollableViewResId != -1) {
            setScrollableView(findViewById(mScrollableViewResId));
        }
    }

    public void setGravity(int gravity) {
        if (gravity != Gravity.TOP && gravity != Gravity.BOTTOM) {
            throw new IllegalArgumentException("gravity must be set to either top or bottom");
        }
        mIsSlidingUp = gravity == Gravity.BOTTOM;
        if (!mFirstLayout) {
            requestLayout();
        }
    }

    /**
     * Set the color used to fade the pane covered by the sliding pane out when the pane
     * will become fully covered in the expanded state.
     *
     * @param color An ARGB-packed color value
     */
    public void setCoveredFadeColor(int color) {
        mCoveredFadeColor = color;
        requestLayout();
    }

View on GitHub (pinned to 45a460435b)