{"record":{"id":"c127d962a37f3176","repo":"material-components/material-components-android","slug":"iconsize-cannot-be-less-than-0","errorCode":null,"errorMessage":"iconSize cannot be less than 0","messagePattern":"iconSize cannot be less than 0","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"lib/java/com/google/android/material/button/MaterialButton.java","lineNumber":954,"sourceCode":"   * @attr ref com.google.android.material.R.styleable#MaterialButton_iconPadding\n   * @see #setIconPadding(int)\n   */\n  @Attribute(\"com.google.android.material:iconPadding\")\n  @Px\n  public int getIconPadding() {\n    return iconPadding;\n  }\n\n  /**\n   * Sets the width and height of the icon. Use 0 to use source Drawable size.\n   *\n   * @param iconSize new dimension for width and height of the icon in pixels.\n   * @attr ref com.google.android.material.R.styleable#MaterialButton_iconSize\n   * @see #getIconSize()\n   */\n  public void setIconSize(@Px int iconSize) {\n    if (iconSize < 0) {\n      throw new IllegalArgumentException(\"iconSize cannot be less than 0\");\n    }\n\n    if (this.iconSize != iconSize) {\n      if (maybeRunAfterWidthAnimation(() -> setIconSize(iconSize))) {\n        return;\n      }\n      originalWidth = UNSET;\n      this.iconSize = iconSize;\n      updateIcon(/* needsIconReset= */ true);\n      updateSecondaryIcon(/* needsIconReset= */ true);\n    }\n  }\n\n  /**\n   * Returns the size of the icon if it was set.\n   *\n   * @return Returns the size of the icon if it was set in pixels, 0 otherwise.\n   * @attr ref com.google.android.material.R.styleable#MaterialButton_iconSize","sourceCodeStart":936,"sourceCodeEnd":972,"githubUrl":"https://github.com/material-components/material-components-android/blob/ac7e18efeefb331850c561faf9ab8bf81d27ba68/lib/java/com/google/android/material/button/MaterialButton.java#L936-L972","documentation":"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.","triggerScenarios":"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.","commonSituations":"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'.","solutions":["Pass 0 to use the drawable's intrinsic size, or a positive pixel dimension.","Convert dp to px correctly (e.g. (int) (dp * getResources().getDisplayMetrics().density)) instead of passing raw dp or sentinel values.","Clamp computed values: Math.max(0, computedSize)."],"exampleFix":"<!-- before -->\n<com.google.android.material.button.MaterialButton\n    app:iconSize=\"-1dp\" ... />\n\n<!-- after -->\n<com.google.android.material.button.MaterialButton\n    app:iconSize=\"24dp\" ... />","handlingStrategy":"validation","validationCode":"int px = Math.max(0, (int) (dp * getResources().getDisplayMetrics().density));\nbutton.setIconSize(px); // 0 = drawable intrinsic size","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never pass -1 sentinels to icon APIs; use 0 for 'intrinsic'.","Convert dp to px at the call site rather than passing raw dp values."],"tags":["material-button","icon","illegal-argument","validation"],"backgroundTag":null,"analyzedSha":"ac7e18efeefb331850c561faf9ab8bf81d27ba68","analyzedAt":"2026-08-14T15:35:35.735Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}