Java Code Examples for androidx.core.view.ViewCompat#LAYOUT_DIRECTION_LTR

The following examples show how to use androidx.core.view.ViewCompat#LAYOUT_DIRECTION_LTR . 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: ChipDrawable.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
/**
 * Calculates the chip text's ChipDrawable-absolute bounds (top-left is <code>
 * [ChipDrawable.getBounds().left, ChipDrawable.getBounds().top]</code>).
 */
private void calculateTextBounds(@NonNull Rect bounds, @NonNull RectF outBounds) {
  outBounds.setEmpty();

  if (text != null) {
    float offsetFromStart = chipStartPadding + calculateChipIconWidth() + textStartPadding;
    float offsetFromEnd = chipEndPadding + calculateCloseIconWidth() + textEndPadding;

    if (DrawableCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_LTR) {
      outBounds.left = bounds.left + offsetFromStart;
      outBounds.right = bounds.right - offsetFromEnd;
    } else {
      outBounds.left = bounds.left + offsetFromEnd;
      outBounds.right = bounds.right - offsetFromStart;
    }

    // Top and bottom included for completion. Don't position the chip text vertically based on
    // these bounds. Instead, use #calculateTextOriginAndAlignment().
    outBounds.top = bounds.top;
    outBounds.bottom = bounds.bottom;
  }
}
 
Example 2
Source File: FastScroll.java    From RecyclerExt with Apache License 2.0 6 votes vote down vote up
/**
 * Determines if the {@link MotionEvent#ACTION_DOWN} event should be ignored.
 * This occurs when the event position is outside the bounds of the drag handle or
 * the track (of the drag handle) when disabled (see {@link #setTrackClicksAllowed(boolean)}
 *
 * @param xPos The x coordinate of the event
 * @param yPos The y coordinate of the event
 * @return {@code true} if the event should be ignored
 */
protected boolean ignoreTouchDown(float xPos, float yPos) {
    //Verifies the event is within the allowed X coordinates
    if (ViewCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_LTR) {
        if (xPos < handle.getX() - ViewCompat.getPaddingStart(handle)) {
            return true;
        }
    } else {
        if (xPos > handle.getX() + handle.getWidth() + ViewCompat.getPaddingStart(handle)) {
            return true;
        }
    }

    if (!trackClicksAllowed) {
        //Enforces selection to only occur on the handle
        if (yPos < handle.getY() - handle.getPaddingTop() || yPos > handle.getY() + handle.getHeight() + handle.getPaddingBottom()) {
            return true;
        }
    }

    return false;
}
 
Example 3
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 4
Source File: ItemTouchHelperExtension.java    From CrazyDaily with Apache License 2.0 6 votes vote down vote up
/**
 * Converts a given set of flags to absolution direction which means {@link #START} and
 * {@link #END} are replaced with {@link #LEFT} and {@link #RIGHT} depending on the layout
 * direction.
 *
 * @param flags           The flag value that include any number of movement flags.
 * @param layoutDirection The layout direction of the RecyclerView.
 * @return Updated flags which includes only absolute direction values.
 */
public int convertToAbsoluteDirection(int flags, int layoutDirection) {
    int masked = flags & RELATIVE_DIR_FLAGS;
    if (masked == 0) {
        return flags;// does not have any relative flags, good.
    }
    flags &= ~masked; //remove start / end
    if (layoutDirection == ViewCompat.LAYOUT_DIRECTION_LTR) {
        // no change. just OR with 2 bits shifted mask and return
        flags |= masked >> 2; // START is 2 bits after LEFT, END is 2 bits after RIGHT.
        return flags;
    } else {
        // add START flag as RIGHT
        flags |= ((masked >> 1) & ~RELATIVE_DIR_FLAGS);
        // first clean start bit then add END flag as LEFT
        flags |= ((masked >> 1) & RELATIVE_DIR_FLAGS) >> 2;
    }
    return flags;
}
 
Example 5
Source File: ItemTouchHelperExtension.java    From CrazyDaily with Apache License 2.0 6 votes vote down vote up
/**
 * Replaces a movement direction with its relative version by taking layout direction into
 * account.
 *
 * @param flags           The flag value that include any number of movement flags.
 * @param layoutDirection The layout direction of the View. Can be obtained from
 *                        {@link ViewCompat#getLayoutDirection(View)}.
 * @return Updated flags which uses relative flags ({@link #START}, {@link #END}) instead
 * of {@link #LEFT}, {@link #RIGHT}.
 * @see #convertToAbsoluteDirection(int, int)
 */
public static int convertToRelativeDirection(int flags, int layoutDirection) {
    int masked = flags & ABS_HORIZONTAL_DIR_FLAGS;
    if (masked == 0) {
        return flags;// does not have any abs flags, good.
    }
    flags &= ~masked; //remove left / right.
    if (layoutDirection == ViewCompat.LAYOUT_DIRECTION_LTR) {
        // no change. just OR with 2 bits shifted mask and return
        flags |= masked << 2; // START is 2 bits after LEFT, END is 2 bits after RIGHT.
        return flags;
    } else {
        // add RIGHT flag as START
        flags |= ((masked << 1) & ~ABS_HORIZONTAL_DIR_FLAGS);
        // first clean RIGHT bit then add LEFT flag as END
        flags |= ((masked << 1) & ABS_HORIZONTAL_DIR_FLAGS) << 2;
    }
    return flags;
}
 
Example 6
Source File: ChipDrawable.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
private void calculateChipTouchBounds(@NonNull Rect bounds, @NonNull RectF outBounds) {
  outBounds.set(bounds);

  if (showsCloseIcon()) {
    float offsetFromEnd =
        chipEndPadding
            + closeIconEndPadding
            + closeIconSize
            + closeIconStartPadding
            + textEndPadding;

    if (DrawableCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_LTR) {
      outBounds.right = bounds.right - offsetFromEnd;
    } else {
      outBounds.left = bounds.left + offsetFromEnd;
    }
  }
}
 
Example 7
Source File: ItemTouchHelper.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Replaces a movement direction with its relative version by taking layout direction into
 * account.
 *
 * @param flags           The flag value that include any number of movement flags.
 * @param layoutDirection The layout direction of the View. Can be obtained from
 *                        {@link ViewCompat#getLayoutDirection(android.view.View)}.
 * @return Updated flags which uses relative flags ({@link #START}, {@link #END}) instead
 * of {@link #LEFT}, {@link #RIGHT}.
 * @see #convertToAbsoluteDirection(int, int)
 */
@SuppressWarnings("WeakerAccess")
public static int convertToRelativeDirection(int flags, int layoutDirection) {
    int masked = flags & ABS_HORIZONTAL_DIR_FLAGS;
    if (masked == 0) {
        return flags; // does not have any abs flags, good.
    }
    flags &= ~masked; //remove left / right.
    if (layoutDirection == ViewCompat.LAYOUT_DIRECTION_LTR) {
        // no change. just OR with 2 bits shifted mask and return
        flags |= masked << 2; // START is 2 bits after LEFT, END is 2 bits after RIGHT.
        return flags;
    } else {
        // add RIGHT flag as START
        flags |= ((masked << 1) & ~ABS_HORIZONTAL_DIR_FLAGS);
        // first clean RIGHT bit then add LEFT flag as END
        flags |= ((masked << 1) & ABS_HORIZONTAL_DIR_FLAGS) << 2;
    }
    return flags;
}
 
Example 8
Source File: ItemTouchHelper.java    From Telegram with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Converts a given set of flags to absolution direction which means {@link #START} and
 * {@link #END} are replaced with {@link #LEFT} and {@link #RIGHT} depending on the layout
 * direction.
 *
 * @param flags           The flag value that include any number of movement flags.
 * @param layoutDirection The layout direction of the RecyclerView.
 * @return Updated flags which includes only absolute direction values.
 */
@SuppressWarnings("WeakerAccess")
public int convertToAbsoluteDirection(int flags, int layoutDirection) {
    int masked = flags & RELATIVE_DIR_FLAGS;
    if (masked == 0) {
        return flags; // does not have any relative flags, good.
    }
    flags &= ~masked; //remove start / end
    if (layoutDirection == ViewCompat.LAYOUT_DIRECTION_LTR) {
        // no change. just OR with 2 bits shifted mask and return
        flags |= masked >> 2; // START is 2 bits after LEFT, END is 2 bits after RIGHT.
        return flags;
    } else {
        // add START flag as RIGHT
        flags |= ((masked >> 1) & ~RELATIVE_DIR_FLAGS);
        // first clean start bit then add END flag as LEFT
        flags |= ((masked >> 1) & RELATIVE_DIR_FLAGS) >> 2;
    }
    return flags;
}
 
Example 9
Source File: ItemTouchHelper.java    From Telegram with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Replaces a movement direction with its relative version by taking layout direction into
 * account.
 *
 * @param flags           The flag value that include any number of movement flags.
 * @param layoutDirection The layout direction of the View. Can be obtained from
 *                        {@link ViewCompat#getLayoutDirection(android.view.View)}.
 * @return Updated flags which uses relative flags ({@link #START}, {@link #END}) instead
 * of {@link #LEFT}, {@link #RIGHT}.
 * @see #convertToAbsoluteDirection(int, int)
 */
@SuppressWarnings("WeakerAccess")
public static int convertToRelativeDirection(int flags, int layoutDirection) {
    int masked = flags & ABS_HORIZONTAL_DIR_FLAGS;
    if (masked == 0) {
        return flags; // does not have any abs flags, good.
    }
    flags &= ~masked; //remove left / right.
    if (layoutDirection == ViewCompat.LAYOUT_DIRECTION_LTR) {
        // no change. just OR with 2 bits shifted mask and return
        flags |= masked << 2; // START is 2 bits after LEFT, END is 2 bits after RIGHT.
        return flags;
    } else {
        // add RIGHT flag as START
        flags |= ((masked << 1) & ~ABS_HORIZONTAL_DIR_FLAGS);
        // first clean RIGHT bit then add LEFT flag as END
        flags |= ((masked << 1) & ABS_HORIZONTAL_DIR_FLAGS) << 2;
    }
    return flags;
}
 
Example 10
Source File: WeSwipeHelper.java    From monero-wallet-android-app with MIT License 6 votes vote down vote up
/**
 * Replaces a movement direction with its relative version by taking layout direction into
 * account.
 *
 * @param flags           The flag value that include any number of movement flags.
 * @param layoutDirection The layout direction of the View. Can be obtained from
 *                        {@link ViewCompat#getLayoutDirection(android.view.View)}.
 * @return Updated flags which uses relative flags ({@link #START}, {@link #END}) instead
 * of {@link #LEFT}, {@link #RIGHT}.
 * @see #convertToAbsoluteDirection(int, int)
 */
public static int convertToRelativeDirection(int flags, int layoutDirection) {
    int masked = flags & ABS_HORIZONTAL_DIR_FLAGS;
    if (masked == 0) {
        return flags; // does not have any abs flags, good.
    }
    flags &= ~masked; //remove left / right.
    if (layoutDirection == ViewCompat.LAYOUT_DIRECTION_LTR) {
        // no change. just OR with 2 bits shifted mask and return
        flags |= masked << 2; // START is 2 bits after LEFT, END is 2 bits after RIGHT.
        return flags;
    } else {
        // add RIGHT flag as START
        flags |= ((masked << 1) & ~ABS_HORIZONTAL_DIR_FLAGS);
        // first clean RIGHT bit then add LEFT flag as END
        flags |= ((masked << 1) & ABS_HORIZONTAL_DIR_FLAGS) << 2;
    }
    return flags;
}
 
Example 11
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 12
Source File: ViewUtil.java    From deltachat-android with GNU General Public License v3.0 5 votes vote down vote up
public static void setLeftMargin(@NonNull View view, int margin) {
  if (ViewCompat.getLayoutDirection(view) == ViewCompat.LAYOUT_DIRECTION_LTR) {
    ((ViewGroup.MarginLayoutParams) view.getLayoutParams()).leftMargin = margin;
  } else {
    ((ViewGroup.MarginLayoutParams) view.getLayoutParams()).rightMargin = margin;
  }
  view.forceLayout();
  view.requestLayout();
}
 
Example 13
Source File: LayoutState.java    From toktok-android with GNU General Public License v3.0 5 votes vote down vote up
public LayoutState(@NonNull RecyclerView.LayoutManager layoutManager, RecyclerView.Recycler recycler,
                   RecyclerView.State recyclerState) {
    viewCache = new SparseArray<>(layoutManager.getChildCount());
    this.recyclerState = recyclerState;
    this.recycler = recycler;
    isLTR = layoutManager.getLayoutDirection() == ViewCompat.LAYOUT_DIRECTION_LTR;
}
 
Example 14
Source File: InputPanel.java    From deltachat-android with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onRecordMoved(float x, float absoluteX) {
  slideToCancel.moveTo(x);

  int   direction = ViewCompat.getLayoutDirection(this);
  float position  = absoluteX / recordingContainer.getWidth();

  if (direction == ViewCompat.LAYOUT_DIRECTION_LTR && position <= 0.5 ||
          direction == ViewCompat.LAYOUT_DIRECTION_RTL && position >= 0.6)
  {
    this.microphoneRecorderView.cancelAction();
  }
}
 
Example 15
Source File: ViewUtil.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
public static void setLeftMargin(@NonNull View view, int margin) {
  if (ViewCompat.getLayoutDirection(view) == ViewCompat.LAYOUT_DIRECTION_LTR) {
    ((ViewGroup.MarginLayoutParams) view.getLayoutParams()).leftMargin = margin;
  } else {
    ((ViewGroup.MarginLayoutParams) view.getLayoutParams()).rightMargin = margin;
  }
  view.forceLayout();
  view.requestLayout();
}
 
Example 16
Source File: ViewUtil.java    From mollyim-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 17
Source File: VoiceRecodingPanel.java    From bcm-android with GNU General Public License v3.0 4 votes vote down vote up
private float getOffset(float x) {
    return ViewCompat.getLayoutDirection(recordButtonFab) == ViewCompat.LAYOUT_DIRECTION_LTR ?
            -Math.max(0, this.startPositionX - x) : Math.max(0, x - this.startPositionX);
}
 
Example 18
Source File: MicrophoneRecorderView.java    From deltachat-android with GNU General Public License v3.0 4 votes vote down vote up
private float getOffset(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: VerticalSeekBarWrapper.java    From MTweaks-KernelAdiutorMOD with GNU General Public License v3.0 4 votes vote down vote up
private void applyViewRotation(int w, int h) {
    final VerticalSeekBar seekBar = getChildSeekBar();

    if (seekBar != null) {
        final boolean isLTR = ViewCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_LTR;
        final int rotationAngle = seekBar.getRotationAngle();
        final int seekBarMeasuredWidth = seekBar.getMeasuredWidth();
        final int seekBarMeasuredHeight = seekBar.getMeasuredHeight();
        final int hPadding = getPaddingLeft() + getPaddingRight();
        final int vPadding = getPaddingTop() + getPaddingBottom();
        final float hOffset = (Math.max(0, w - hPadding) - seekBarMeasuredHeight) * 0.5f;
        final ViewGroup.LayoutParams lp = seekBar.getLayoutParams();

        lp.width = Math.max(0, h - vPadding);
        lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;

        seekBar.setLayoutParams(lp);

        seekBar.setPivotX((isLTR) ? 0 : Math.max(0, h - vPadding));
        seekBar.setPivotY(0);

        switch (rotationAngle) {
            case VerticalSeekBar.ROTATION_ANGLE_CW_90:
                seekBar.setRotation(90);
                if (isLTR) {
                    seekBar.setTranslationX(seekBarMeasuredHeight + hOffset);
                    seekBar.setTranslationY(0);
                } else {
                    seekBar.setTranslationX(-hOffset);
                    seekBar.setTranslationY(seekBarMeasuredWidth);
                }
                break;
            case VerticalSeekBar.ROTATION_ANGLE_CW_270:
                seekBar.setRotation( 270);
                if (isLTR) {
                    seekBar.setTranslationX(hOffset);
                    seekBar.setTranslationY(seekBarMeasuredWidth);
                } else {
                    seekBar.setTranslationX(-(seekBarMeasuredHeight + hOffset));
                    seekBar.setTranslationY(0);
                }
                break;
        }
    }
}
 
Example 20
Source File: ViewUtil.java    From mollyim-android with GNU General Public License v3.0 4 votes vote down vote up
public static int getRightMargin(@NonNull View view) {
  if (ViewCompat.getLayoutDirection(view) == ViewCompat.LAYOUT_DIRECTION_LTR) {
    return ((ViewGroup.MarginLayoutParams) view.getLayoutParams()).rightMargin;
  }
  return ((ViewGroup.MarginLayoutParams) view.getLayoutParams()).leftMargin;
}