/**
   * Measure a child view to fit within cell-based formatting. The child's width will be measured to
   * a whole multiple of cellSize.
   *
   * <p>Sets the expandable and cellsUsed fields of LayoutParams.
   *
   * @param child Child to measure
   * @param cellSize Size of one cell
   * @param cellsRemaining Number of cells remaining that this view can expand to fill
   * @param parentHeightMeasureSpec MeasureSpec used by the parent view
   * @param parentHeightPadding Padding present in the parent view
   * @return Number of cells this child was measured to occupy
   */
  static int measureChildForCells(
      View child,
      int cellSize,
      int cellsRemaining,
      int parentHeightMeasureSpec,
      int parentHeightPadding) {
    final LayoutParams lp = (LayoutParams) child.getLayoutParams();

    final int childHeightSize = MeasureSpec.getSize(parentHeightMeasureSpec) - parentHeightPadding;
    final int childHeightMode = MeasureSpec.getMode(parentHeightMeasureSpec);
    final int childHeightSpec = MeasureSpec.makeMeasureSpec(childHeightSize, childHeightMode);

    final ActionMenuItemView itemView =
        child instanceof ActionMenuItemView ? (ActionMenuItemView) child : null;
    final boolean hasText = itemView != null && itemView.hasText();

    int cellsUsed = 0;
    if (cellsRemaining > 0 && (!hasText || cellsRemaining >= 2)) {
      final int childWidthSpec =
          MeasureSpec.makeMeasureSpec(cellSize * cellsRemaining, MeasureSpec.AT_MOST);
      child.measure(childWidthSpec, childHeightSpec);

      final int measuredWidth = child.getMeasuredWidth();
      cellsUsed = measuredWidth / cellSize;
      if (measuredWidth % cellSize != 0) cellsUsed++;
      if (hasText && cellsUsed < 2) cellsUsed = 2;
    }

    final boolean expandable = !lp.isOverflowButton && hasText;
    lp.expandable = expandable;

    lp.cellsUsed = cellsUsed;
    final int targetWidth = cellsUsed * cellSize;
    child.measure(MeasureSpec.makeMeasureSpec(targetWidth, MeasureSpec.EXACTLY), childHeightSpec);
    return cellsUsed;
  }