Java Code Examples for androidx.core.view.MarginLayoutParamsCompat#getMarginStart()

The following examples show how to use androidx.core.view.MarginLayoutParamsCompat#getMarginStart() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Utils.java    From SmartTabLayout with Apache License 2.0 5 votes vote down vote up
static int getMarginStart(View v) {
  if (v == null) {
    return 0;
  }
  ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
  return MarginLayoutParamsCompat.getMarginStart(lp);
}
 
Example 2
Source File: Utils.java    From SmartTabLayout with Apache License 2.0 5 votes vote down vote up
static int getMarginHorizontally(View v) {
  if (v == null) {
    return 0;
  }
  ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
  return MarginLayoutParamsCompat.getMarginStart(lp) + MarginLayoutParamsCompat.getMarginEnd(lp);
}
 
Example 3
Source File: FlowLayout.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
@Override
protected void onLayout(boolean sizeChanged, int left, int top, int right, int bottom) {
  if (getChildCount() == 0) {
    // Do not re-layout when there are no children.
    rowCount = 0;
    return;
  }
  rowCount = 1;

  boolean isRtl = ViewCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_RTL;
  int paddingStart = isRtl ? getPaddingRight() : getPaddingLeft();
  int paddingEnd = isRtl ? getPaddingLeft() : getPaddingRight();
  int childStart = paddingStart;
  int childTop = getPaddingTop();
  int childBottom = childTop;
  int childEnd;

  final int maxChildEnd = right - left - paddingEnd;

  for (int i = 0; i < getChildCount(); i++) {
    View child = getChildAt(i);

    if (child.getVisibility() == View.GONE) {
      child.setTag(R.id.row_index_key, -1);
      continue;
    }

    LayoutParams lp = child.getLayoutParams();
    int startMargin = 0;
    int endMargin = 0;
    if (lp instanceof MarginLayoutParams) {
      MarginLayoutParams marginLp = (MarginLayoutParams) lp;
      startMargin = MarginLayoutParamsCompat.getMarginStart(marginLp);
      endMargin = MarginLayoutParamsCompat.getMarginEnd(marginLp);
    }

    childEnd = childStart + startMargin + child.getMeasuredWidth();

    if (!singleLine && (childEnd > maxChildEnd)) {
      childStart = paddingStart;
      childTop = childBottom + lineSpacing;
      rowCount++;
    }
    child.setTag(R.id.row_index_key, rowCount - 1);

    childEnd = childStart + startMargin + child.getMeasuredWidth();
    childBottom = childTop + child.getMeasuredHeight();

    if (isRtl) {
      child.layout(
          maxChildEnd - childEnd, childTop, maxChildEnd - childStart - startMargin, childBottom);
    } else {
      child.layout(childStart + startMargin, childTop, childEnd, childBottom);
    }

    childStart += (startMargin + endMargin + child.getMeasuredWidth()) + itemSpacing;
  }
}