Java Code Examples for androidx.core.view.ViewCompat#getLayoutDirection()

The following examples show how to use androidx.core.view.ViewCompat#getLayoutDirection() . 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: TabLayout.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
private int calculateScrollXForTab(int position, float positionOffset) {
  if (mode == MODE_SCROLLABLE || mode == MODE_AUTO) {
    final View selectedChild = slidingTabIndicator.getChildAt(position);
    final View nextChild =
        position + 1 < slidingTabIndicator.getChildCount()
            ? slidingTabIndicator.getChildAt(position + 1)
            : null;
    final int selectedWidth = selectedChild != null ? selectedChild.getWidth() : 0;
    final int nextWidth = nextChild != null ? nextChild.getWidth() : 0;

    // base scroll amount: places center of tab in center of parent
    int scrollBase = selectedChild.getLeft() + (selectedWidth / 2) - (getWidth() / 2);
    // offset amount: fraction of the distance between centers of tabs
    int scrollOffset = (int) ((selectedWidth + nextWidth) * 0.5f * positionOffset);

    return (ViewCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_LTR)
        ? scrollBase + scrollOffset
        : scrollBase - scrollOffset;
  }
  return 0;
}
 
Example 2
Source File: CustomItemLayout.java    From PagerBottomTabStrip with Apache License 2.0 6 votes vote down vote up
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    final int count = getChildCount();
    final int width = right - left;
    final int height = bottom - top;
    //只支持top、bottom的padding
    final int padding_top = getPaddingTop();
    final int padding_bottom = getPaddingBottom();
    int used = 0;

    for (int i = 0; i < count; i++) {
        final View child = getChildAt(i);
        if (child.getVisibility() == GONE) {
            continue;
        }
        if (ViewCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_RTL) {
            child.layout(width - used - child.getMeasuredWidth(), padding_top, width - used, height - padding_bottom);
        } else {
            child.layout(used, padding_top, child.getMeasuredWidth() + used, height - padding_bottom);
        }
        used += child.getMeasuredWidth();
    }
}
 
Example 3
Source File: MaterialCardViewHelper.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
void onMeasure(int measuredWidth, int measuredHeight) {
  if (clickableForegroundDrawable != null) {
    int left = measuredWidth - checkedIconMargin - checkedIconSize;
    int bottom = measuredHeight - checkedIconMargin - checkedIconSize;
    boolean isPreLollipop = VERSION.SDK_INT < VERSION_CODES.LOLLIPOP;
    if (isPreLollipop || materialCardView.getUseCompatPadding()) {
      bottom -= (int) Math.ceil(2f * calculateVerticalBackgroundPadding());
      left -= (int) Math.ceil(2f * calculateHorizontalBackgroundPadding());
    }

    int right = checkedIconMargin;
    if (ViewCompat.getLayoutDirection(materialCardView) == ViewCompat.LAYOUT_DIRECTION_RTL) {
      // swap left and right
      int tmp = right;
      right = left;
      left = tmp;
    }

    clickableForegroundDrawable.setLayerInset(
        CHECKED_ICON_LAYER_INDEX, left, checkedIconMargin /* top */, right, bottom);
  }
}
 
Example 4
Source File: SetupStartIndicatorView.java    From AOSP-Kayboard-7.1.2 with Apache License 2.0 6 votes vote down vote up
@Override
protected void onDraw(final Canvas canvas) {
    super.onDraw(canvas);
    final int layoutDirection = ViewCompat.getLayoutDirection(this);
    final int width = getWidth();
    final int height = getHeight();
    final float halfHeight = height / 2.0f;
    final Path path = mIndicatorPath;
    path.rewind();
    if (layoutDirection == ViewCompat.LAYOUT_DIRECTION_RTL) {
        // Left arrow
        path.moveTo(width, 0.0f);
        path.lineTo(0.0f, halfHeight);
        path.lineTo(width, height);
    } else { // LAYOUT_DIRECTION_LTR
        // Right arrow
        path.moveTo(0.0f, 0.0f);
        path.lineTo(width, halfHeight);
        path.lineTo(0.0f, height);
    }
    path.close();
    final int[] stateSet = getDrawableState();
    final int color = mIndicatorColor.getColorForState(stateSet, 0);
    mIndicatorPaint.setColor(color);
    canvas.drawPath(path, mIndicatorPaint);
}
 
Example 5
Source File: SnackbarTest.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
@Test
public void testDismissViaSwipeRtl() throws Throwable {
  onView(withId(R.id.col)).perform(setLayoutDirection(ViewCompat.LAYOUT_DIRECTION_RTL));
  if (ViewCompat.getLayoutDirection(coordinatorLayout) == ViewCompat.LAYOUT_DIRECTION_RTL) {
    // On devices that support RTL layout, the start-to-end dismiss swipe is done
    // with swipeLeft() action
    verifyDismissCallback(
        onView(isAssignableFrom(Snackbar.SnackbarLayout.class)),
        swipeLeft(),
        null,
        Snackbar.LENGTH_INDEFINITE,
        Snackbar.Callback.DISMISS_EVENT_SWIPE);
  }
}
 
Example 6
Source File: CollapsingTextHelper.java    From UIWidget with Apache License 2.0 5 votes vote down vote up
private boolean calculateIsRtl(CharSequence text) {
    final boolean defaultIsRtl = ViewCompat.getLayoutDirection(mView)
            == ViewCompat.LAYOUT_DIRECTION_RTL;
    return (defaultIsRtl
            ? TextDirectionHeuristicsCompat.FIRSTSTRONG_RTL
            : TextDirectionHeuristicsCompat.FIRSTSTRONG_LTR).isRtl(text, 0, text.length());
}
 
Example 7
Source File: ColorPanelView.java    From ColorPicker with Apache License 2.0 5 votes vote down vote up
/**
 * Show a toast message with the hex color code below the view.
 */
public void showHint() {
  final int[] screenPos = new int[2];
  final Rect displayFrame = new Rect();
  getLocationOnScreen(screenPos);
  getWindowVisibleDisplayFrame(displayFrame);
  final Context context = getContext();
  final int width = getWidth();
  final int height = getHeight();
  final int midy = screenPos[1] + height / 2;
  int referenceX = screenPos[0] + width / 2;
  if (ViewCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_LTR) {
    final int screenWidth = context.getResources().getDisplayMetrics().widthPixels;
    referenceX = screenWidth - referenceX; // mirror
  }
  StringBuilder hint = new StringBuilder("#");
  if (Color.alpha(color) != 255) {
    hint.append(Integer.toHexString(color).toUpperCase(Locale.ENGLISH));
  } else {
    hint.append(String.format("%06X", 0xFFFFFF & color).toUpperCase(Locale.ENGLISH));
  }
  Toast cheatSheet = Toast.makeText(context, hint.toString(), Toast.LENGTH_SHORT);
  if (midy < displayFrame.height()) {
    // Show along the top; follow action buttons
    cheatSheet.setGravity(Gravity.TOP | GravityCompat.END, referenceX, screenPos[1] + height - displayFrame.top);
  } else {
    // Show along the bottom center
    cheatSheet.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, height);
  }
  cheatSheet.show();
}
 
Example 8
Source File: MarginView.java    From Carbon with Apache License 2.0 5 votes vote down vote up
default void setMarginEnd(int margin) {
    View view = (View) this;
    ViewGroup.LayoutParams params = view.getLayoutParams();
    if (!(params instanceof ViewGroup.MarginLayoutParams))
        throw new IllegalStateException("Invalid layoutParams. Unable to set margin.");
    ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) params;
    int layoutDirection = ViewCompat.getLayoutDirection(view);
    if (layoutDirection == ViewCompat.LAYOUT_DIRECTION_LTR) {
        layoutParams.setMargins(layoutParams.leftMargin, layoutParams.topMargin, margin, layoutParams.bottomMargin);
    } else {
        layoutParams.setMargins(margin, layoutParams.topMargin, layoutParams.rightMargin, layoutParams.bottomMargin);
    }
    view.setLayoutParams(layoutParams);
}
 
Example 9
Source File: VoiceRecodingPanel.java    From bcm-android with GNU General Public License v3.0 4 votes vote down vote up
private int getWidthAdjustment() {
    int width = recordButtonFab.getWidth() / 4;
    return ViewCompat.getLayoutDirection(recordButtonFab) == ViewCompat.LAYOUT_DIRECTION_LTR ? -width : width;
}
 
Example 10
Source File: BadgeDrawable.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
private void calculateCenterAndBounds(
    @NonNull Context context, @NonNull Rect anchorRect, @NonNull View anchorView) {
  switch (savedState.badgeGravity) {
    case BOTTOM_END:
    case BOTTOM_START:
      badgeCenterY = anchorRect.bottom - savedState.verticalOffset;
      break;
    case TOP_END:
    case TOP_START:
    default:
      badgeCenterY = anchorRect.top + savedState.verticalOffset;
      break;
  }

  if (getNumber() <= MAX_CIRCULAR_BADGE_NUMBER_COUNT) {
    cornerRadius = !hasNumber() ? badgeRadius : badgeWithTextRadius;
    halfBadgeHeight = cornerRadius;
    halfBadgeWidth = cornerRadius;
  } else {
    cornerRadius = badgeWithTextRadius;
    halfBadgeHeight = cornerRadius;
    String badgeText = getBadgeText();
    halfBadgeWidth = textDrawableHelper.getTextWidth(badgeText) / 2f + badgeWidePadding;
  }

  int inset =
      context
          .getResources()
          .getDimensionPixelSize(
              hasNumber()
                  ? R.dimen.mtrl_badge_text_horizontal_edge_offset
                  : R.dimen.mtrl_badge_horizontal_edge_offset);
  // Update the centerX based on the badge width and 'inset' from start or end boundary of anchor.
  switch (savedState.badgeGravity) {
    case BOTTOM_START:
    case TOP_START:
      badgeCenterX =
          ViewCompat.getLayoutDirection(anchorView) == View.LAYOUT_DIRECTION_LTR
              ? anchorRect.left - halfBadgeWidth + inset + savedState.horizontalOffset
              : anchorRect.right + halfBadgeWidth - inset - savedState.horizontalOffset;
      break;
    case BOTTOM_END:
    case TOP_END:
    default:
      badgeCenterX =
          ViewCompat.getLayoutDirection(anchorView) == View.LAYOUT_DIRECTION_LTR
              ? anchorRect.right + halfBadgeWidth - inset - savedState.horizontalOffset
              : anchorRect.left - halfBadgeWidth + inset + savedState.horizontalOffset;
      break;
  }
}
 
Example 11
Source File: StepView.java    From StepView with Apache License 2.0 4 votes vote down vote up
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private boolean isRtl() {
    return ViewCompat.getLayoutDirection(this) == View.LAYOUT_DIRECTION_RTL;
}
 
Example 12
Source File: ViewUtil.java    From deltachat-android with GNU General Public License v3.0 4 votes vote down vote up
public static int getLeftMargin(@NonNull View view) {
  if (ViewCompat.getLayoutDirection(view) == ViewCompat.LAYOUT_DIRECTION_LTR) {
    return ((ViewGroup.MarginLayoutParams) view.getLayoutParams()).leftMargin;
  }
  return ((ViewGroup.MarginLayoutParams) view.getLayoutParams()).rightMargin;
}
 
Example 13
Source File: DropDown.java    From Carbon with Apache License 2.0 4 votes vote down vote up
private boolean isLayoutRtl() {
    return ViewCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_RTL;
}
 
Example 14
Source File: RadioButton.java    From Carbon with Apache License 2.0 4 votes vote down vote up
private boolean isLayoutRtl() {
    return ViewCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_RTL;
}
 
Example 15
Source File: MaterialButton.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
private boolean isLayoutRTL() {
  return ViewCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_RTL;
}
 
Example 16
Source File: DynamicPageIndicator2.java    From dynamic-support with Apache License 2.0 4 votes vote down vote up
private boolean isRTL() {
    return ViewCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_RTL;
}
 
Example 17
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;
  }
}
 
Example 18
Source File: MicrophoneRecorderView.java    From mollyim-android with GNU General Public License v3.0 4 votes vote down vote up
private float getXOffset(float x) {
  return ViewCompat.getLayoutDirection(recordButtonFab) == ViewCompat.LAYOUT_DIRECTION_LTR ?
      -Math.max(0, this.startPositionX - x) : Math.max(0, x - this.startPositionX);
}
 
Example 19
Source File: ViewUtils.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
public static boolean isLayoutRtl(View view) {
  return ViewCompat.getLayoutDirection(view) == ViewCompat.LAYOUT_DIRECTION_RTL;
}
 
Example 20
Source File: ViewUtils.java    From firefox-echo-show with Mozilla Public License 2.0 4 votes vote down vote up
public static boolean isRTL(View view) {
    return ViewCompat.getLayoutDirection(view) == ViewCompat.LAYOUT_DIRECTION_RTL;
}