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

iconSize cannot be less than 0

Error message

iconSize cannot be less than 0

What it means

MaterialButton.setIconSize(int) sets the width and height of the button's (primary) icon in pixels, where 0 means 'use the drawable's intrinsic size'. Negative sizes are meaningless and rejected immediately with IllegalArgumentException before any icon update happens.

Source

Thrown at lib/java/com/google/android/material/button/MaterialButton.java:954

   * @attr ref com.google.android.material.R.styleable#MaterialButton_iconPadding
   * @see #setIconPadding(int)
   */
  @Attribute("com.google.android.material:iconPadding")
  @Px
  public int getIconPadding() {
    return iconPadding;
  }

  /**
   * Sets the width and height of the icon. Use 0 to use source Drawable size.
   *
   * @param iconSize new dimension for width and height of the icon in pixels.
   * @attr ref com.google.android.material.R.styleable#MaterialButton_iconSize
   * @see #getIconSize()
   */
  public void setIconSize(@Px int iconSize) {
    if (iconSize < 0) {
      throw new IllegalArgumentException("iconSize cannot be less than 0");
    }

    if (this.iconSize != iconSize) {
      if (maybeRunAfterWidthAnimation(() -> setIconSize(iconSize))) {
        return;
      }
      originalWidth = UNSET;
      this.iconSize = iconSize;
      updateIcon(/* needsIconReset= */ true);
      updateSecondaryIcon(/* needsIconReset= */ true);
    }
  }

  /**
   * Returns the size of the icon if it was set.
   *
   * @return Returns the size of the icon if it was set in pixels, 0 otherwise.
   * @attr ref com.google.android.material.R.styleable#MaterialButton_iconSize

View on GitHub (pinned to ac7e18efee)

Solutions

  1. Pass 0 to use the drawable's intrinsic size, or a positive pixel dimension.
  2. Convert dp to px correctly (e.g. (int) (dp * getResources().getDisplayMetrics().density)) instead of passing raw dp or sentinel values.
  3. Clamp computed values: Math.max(0, computedSize).

Example fix

<!-- before -->
<com.google.android.material.button.MaterialButton
    app:iconSize="-1dp" ... />

<!-- after -->
<com.google.android.material.button.MaterialButton
    app:iconSize="24dp" ... />
Defensive patterns

Strategy: validation

Validate before calling

int px = Math.max(0, (int) (dp * getResources().getDisplayMetrics().density));
button.setIconSize(px); // 0 = drawable intrinsic size

Prevention

When it happens

Trigger: Calling setIconSize(-1) or any negative int; setting app:iconSize in XML with a dimension that resolves negative; computing the size at runtime (e.g. from display metrics or user preference scaling) that evaluates below zero.

Common situations: Passing an unset/initialized-to--1 sentinel value by mistake; icon size derived from a fraction of screen width that underflows on unusual configurations; copying a size variable from another context where -1 meant 'unspecified'.

Related errors


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