{"record":{"id":"8396d8099a888ff6","repo":"material-components/material-components-android","slug":"ratio-must-be-a-float-value-between-0-and-1","errorCode":null,"errorMessage":"ratio must be a float value between 0 and 1","messagePattern":"ratio must be a float value between 0 and 1","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"lib/java/com/google/android/material/bottomsheet/BottomSheetBehavior.java","lineNumber":1168,"sourceCode":"  public int getPeekHeight() {\n    return peekHeightAuto ? PEEK_HEIGHT_AUTO : peekHeight;\n  }\n\n  /**\n   * Determines the height of the BottomSheet in the {@link #STATE_HALF_EXPANDED} state. The\n   * material guidelines recommended a value of 0.5, which results in the sheet filling half of the\n   * parent. The height of the BottomSheet will be smaller as this ratio is decreased and taller as\n   * it is increased. The default value is 0.5.\n   *\n   * @param ratio a float between 0 and 1, representing the {@link #STATE_HALF_EXPANDED} ratio.\n   * @attr ref\n   *     com.google.android.material.R.styleable#BottomSheetBehavior_Layout_behavior_halfExpandedRatio\n   */\n  public void setHalfExpandedRatio(\n      @FloatRange(from = 0.0f, to = 1.0f, fromInclusive = false, toInclusive = false) float ratio) {\n\n    if ((ratio <= 0) || (ratio >= 1)) {\n      throw new IllegalArgumentException(\"ratio must be a float value between 0 and 1\");\n    }\n    this.halfExpandedRatio = ratio;\n    // If sheet is already laid out, recalculate the half expanded offset based on new setting.\n    // Otherwise, let onLayoutChild handle this later.\n    if (viewRef != null) {\n      calculateHalfExpandedOffset();\n    }\n  }\n\n  /**\n   * Gets the ratio for the height of the BottomSheet in the {@link #STATE_HALF_EXPANDED} state.\n   *\n   * @attr ref\n   *     com.google.android.material.R.styleable#BottomSheetBehavior_Layout_behavior_halfExpandedRatio\n   */\n  @FloatRange(from = 0.0f, to = 1.0f)\n  public float getHalfExpandedRatio() {\n    return halfExpandedRatio;","sourceCodeStart":1150,"sourceCodeEnd":1186,"githubUrl":"https://github.com/material-components/material-components-android/blob/ac7e18efeefb331850c561faf9ab8bf81d27ba68/lib/java/com/google/android/material/bottomsheet/BottomSheetBehavior.java#L1150-L1186","documentation":"BottomSheetBehavior.setHalfExpandedRatio(float) sets what fraction of the parent height the sheet occupies in STATE_HALF_EXPANDED. The value is annotated @FloatRange(from=0, to=1, fromInclusive=false, toInclusive=false) — it must be strictly between 0 and 1; 0.0f, 1.0f, negative values, and values above 1 all throw IllegalArgumentException.","triggerScenarios":"Calling setHalfExpandedRatio(0f) or setHalfExpandedRatio(1f) exactly; deriving the ratio from a computation (sheetHeight / parentHeight) that evaluates to 0 or 1 on tiny or full-height content; loading the ratio from a config value that defaults to 0.","commonSituations":"Computing the ratio dynamically at runtime so edge-case layouts (0-height or full-height sheets) produce exactly 0 or 1; storing the ratio in remote config/backend with an unset 0 default; forgetting that both endpoints are exclusive.","solutions":["Pass a value strictly inside (0, 1), e.g. the default 0.5f.","Clamp computed values away from the endpoints: Math.min(Math.max(ratio, 0.01f), 0.99f).","Validate persisted/remote values before applying them to the behavior."],"exampleFix":"// before\nbehavior.setHalfExpandedRatio(sheetHeight / parentHeight); // 1.0 when full height\n\n// after\nfloat ratio = Math.min(Math.max(sheetHeight / (float) parentHeight, 0.01f), 0.99f);\nbehavior.setHalfExpandedRatio(ratio);","handlingStrategy":"validation","validationCode":"float ratio = Math.min(Math.max(desiredRatio, Float.MIN_VALUE), 0.9999f);\nbehavior.setHalfExpandedRatio(ratio); // strictly within (0,1)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Remember both endpoints are exclusive.","Validate remote-config/persisted ratio values before applying."],"tags":["bottomsheet","illegal-argument","validation","range-check"],"backgroundTag":null,"analyzedSha":"ac7e18efeefb331850c561faf9ab8bf81d27ba68","analyzedAt":"2026-08-14T15:35:35.735Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}