mirror of
https://github.com/material-components/material-components-android.git
synced 2026-02-20 08:39:55 +08:00
[TextInputLayout][a11y] Fixed RTL support for setting corner radius on TextInputLayout.
PiperOrigin-RevId: 408621523
This commit is contained in:
parent
c6be59ce8b
commit
dca7fc0701
@ -309,6 +309,20 @@ class DropdownMenuEndIconDelegate extends EndIconDelegate {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* This method should be called if the outlined ripple background should be updated. For example,
|
||||
* if a new {@link ShapeAppearanceModel} is set on the text field.
|
||||
*/
|
||||
void updateOutlinedRippleEffect(@NonNull AutoCompleteTextView editText) {
|
||||
if (isEditable(editText)
|
||||
|| textInputLayout.getBoxBackgroundMode() != TextInputLayout.BOX_BACKGROUND_OUTLINE
|
||||
|| !(editText.getBackground() instanceof LayerDrawable)) {
|
||||
return;
|
||||
}
|
||||
|
||||
addRippleEffect(editText);
|
||||
}
|
||||
|
||||
/* Add ripple effect to non editable layouts. */
|
||||
private void addRippleEffect(@NonNull AutoCompleteTextView editText) {
|
||||
if (isEditable(editText)) {
|
||||
|
||||
@ -240,6 +240,7 @@ public class TextInputLayout extends LinearLayout {
|
||||
@Nullable private MaterialShapeDrawable boxBackground;
|
||||
@Nullable private MaterialShapeDrawable boxUnderline;
|
||||
@NonNull private ShapeAppearanceModel shapeAppearanceModel;
|
||||
private boolean areCornerRadiiRtl;
|
||||
|
||||
private final int boxLabelCutoutPaddingPx;
|
||||
@BoxBackgroundMode private int boxBackgroundMode;
|
||||
@ -1292,17 +1293,26 @@ public class TextInputLayout extends LinearLayout {
|
||||
float boxCornerRadiusTopEnd,
|
||||
float boxCornerRadiusBottomStart,
|
||||
float boxCornerRadiusBottomEnd) {
|
||||
areCornerRadiiRtl = ViewUtils.isLayoutRtl(this);
|
||||
float boxCornerRadiusTopLeft =
|
||||
areCornerRadiiRtl ? boxCornerRadiusTopEnd : boxCornerRadiusTopStart;
|
||||
float boxCornerRadiusTopRight =
|
||||
areCornerRadiiRtl ? boxCornerRadiusTopStart : boxCornerRadiusTopEnd;
|
||||
float boxCornerRadiusBottomLeft =
|
||||
areCornerRadiiRtl ? boxCornerRadiusBottomEnd : boxCornerRadiusBottomStart;
|
||||
float boxCornerRadiusBottomRight =
|
||||
areCornerRadiiRtl ? boxCornerRadiusBottomStart : boxCornerRadiusBottomEnd;
|
||||
if (boxBackground == null
|
||||
|| boxBackground.getTopLeftCornerResolvedSize() != boxCornerRadiusTopStart
|
||||
|| boxBackground.getTopRightCornerResolvedSize() != boxCornerRadiusTopEnd
|
||||
|| boxBackground.getBottomLeftCornerResolvedSize() != boxCornerRadiusBottomStart
|
||||
|| boxBackground.getBottomRightCornerResolvedSize() != boxCornerRadiusBottomEnd) {
|
||||
|| boxBackground.getTopLeftCornerResolvedSize() != boxCornerRadiusTopLeft
|
||||
|| boxBackground.getTopRightCornerResolvedSize() != boxCornerRadiusTopRight
|
||||
|| boxBackground.getBottomLeftCornerResolvedSize() != boxCornerRadiusBottomLeft
|
||||
|| boxBackground.getBottomRightCornerResolvedSize() != boxCornerRadiusBottomRight) {
|
||||
shapeAppearanceModel =
|
||||
shapeAppearanceModel.toBuilder()
|
||||
.setTopLeftCornerSize(boxCornerRadiusTopStart)
|
||||
.setTopRightCornerSize(boxCornerRadiusTopEnd)
|
||||
.setBottomLeftCornerSize(boxCornerRadiusBottomStart)
|
||||
.setBottomRightCornerSize(boxCornerRadiusBottomEnd)
|
||||
.setTopLeftCornerSize(boxCornerRadiusTopLeft)
|
||||
.setTopRightCornerSize(boxCornerRadiusTopRight)
|
||||
.setBottomLeftCornerSize(boxCornerRadiusBottomLeft)
|
||||
.setBottomRightCornerSize(boxCornerRadiusBottomRight)
|
||||
.build();
|
||||
applyBoxAttributes();
|
||||
}
|
||||
@ -1315,7 +1325,9 @@ public class TextInputLayout extends LinearLayout {
|
||||
* @see #setBoxCornerRadii(float, float, float, float)
|
||||
*/
|
||||
public float getBoxCornerRadiusTopStart() {
|
||||
return boxBackground.getTopLeftCornerResolvedSize();
|
||||
return ViewUtils.isLayoutRtl(this)
|
||||
? shapeAppearanceModel.getTopRightCornerSize().getCornerSize(tmpRectF)
|
||||
: shapeAppearanceModel.getTopLeftCornerSize().getCornerSize(tmpRectF);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1325,7 +1337,9 @@ public class TextInputLayout extends LinearLayout {
|
||||
* @see #setBoxCornerRadii(float, float, float, float)
|
||||
*/
|
||||
public float getBoxCornerRadiusTopEnd() {
|
||||
return boxBackground.getTopRightCornerResolvedSize();
|
||||
return ViewUtils.isLayoutRtl(this)
|
||||
? shapeAppearanceModel.getTopLeftCornerSize().getCornerSize(tmpRectF)
|
||||
: shapeAppearanceModel.getTopRightCornerSize().getCornerSize(tmpRectF);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1335,7 +1349,9 @@ public class TextInputLayout extends LinearLayout {
|
||||
* @see #setBoxCornerRadii(float, float, float, float)
|
||||
*/
|
||||
public float getBoxCornerRadiusBottomEnd() {
|
||||
return boxBackground.getBottomRightCornerResolvedSize();
|
||||
return ViewUtils.isLayoutRtl(this)
|
||||
? shapeAppearanceModel.getBottomLeftCornerSize().getCornerSize(tmpRectF)
|
||||
: shapeAppearanceModel.getBottomRightCornerSize().getCornerSize(tmpRectF);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1345,7 +1361,9 @@ public class TextInputLayout extends LinearLayout {
|
||||
* @see #setBoxCornerRadii(float, float, float, float)
|
||||
*/
|
||||
public float getBoxCornerRadiusBottomStart() {
|
||||
return boxBackground.getBottomLeftCornerResolvedSize();
|
||||
return ViewUtils.isLayoutRtl(this)
|
||||
? shapeAppearanceModel.getBottomRightCornerSize().getCornerSize(tmpRectF)
|
||||
: shapeAppearanceModel.getBottomLeftCornerSize().getCornerSize(tmpRectF);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -2698,7 +2716,7 @@ public class TextInputLayout extends LinearLayout {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
Rect bounds = tmpBoundsRect;
|
||||
boolean isRtl = ViewCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_RTL;
|
||||
boolean isRtl = ViewUtils.isLayoutRtl(this);
|
||||
|
||||
bounds.bottom = rect.bottom;
|
||||
switch (boxBackgroundMode) {
|
||||
@ -2800,7 +2818,12 @@ public class TextInputLayout extends LinearLayout {
|
||||
return;
|
||||
}
|
||||
|
||||
boxBackground.setShapeAppearanceModel(shapeAppearanceModel);
|
||||
if (boxBackground.getShapeAppearanceModel() != shapeAppearanceModel) {
|
||||
boxBackground.setShapeAppearanceModel(shapeAppearanceModel);
|
||||
// The outlined background of the dropdown menu is created in the end icon delegate, so it
|
||||
// needs to be updated based on the new shape appearance model.
|
||||
updateDropdownMenuBackground();
|
||||
}
|
||||
|
||||
if (canDrawOutlineStroke()) {
|
||||
boxBackground.setStroke(boxStrokeWidthPx, boxStrokeColor);
|
||||
@ -2836,6 +2859,17 @@ public class TextInputLayout extends LinearLayout {
|
||||
return boxStrokeWidthPx > -1 && boxStrokeColor != Color.TRANSPARENT;
|
||||
}
|
||||
|
||||
/*
|
||||
* This method should be called if the outlined ripple background should be updated. For example,
|
||||
* if a new {@link ShapeAppearanceModel} is set on the text field.
|
||||
*/
|
||||
private void updateDropdownMenuBackground() {
|
||||
if (endIconMode == END_ICON_DROPDOWN_MENU && boxBackgroundMode == BOX_BACKGROUND_OUTLINE) {
|
||||
((DropdownMenuEndIconDelegate) endIconDelegates.get(END_ICON_DROPDOWN_MENU))
|
||||
.updateOutlinedRippleEffect((AutoCompleteTextView) editText);
|
||||
}
|
||||
}
|
||||
|
||||
void updateEditTextBackground() {
|
||||
// Only update the color filter for the legacy text field, since we can directly change the
|
||||
// Paint colors of the MaterialShapeDrawable box background without having to use color filters.
|
||||
@ -3057,6 +3091,30 @@ public class TextInputLayout extends LinearLayout {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRtlPropertiesChanged(int layoutDirection) {
|
||||
super.onRtlPropertiesChanged(layoutDirection);
|
||||
boolean isLayoutDirectionRtl = layoutDirection == ViewCompat.LAYOUT_DIRECTION_RTL;
|
||||
if (isLayoutDirectionRtl != areCornerRadiiRtl) {
|
||||
// Switch corner radius values from LTR to RTL or vice versa.
|
||||
boolean shouldCornersBeRtl = isLayoutDirectionRtl && !areCornerRadiiRtl;
|
||||
float boxCornerRadiusTopLeft =
|
||||
shapeAppearanceModel.getTopLeftCornerSize().getCornerSize(tmpRectF);
|
||||
float boxCornerRadiusTopRight =
|
||||
shapeAppearanceModel.getTopRightCornerSize().getCornerSize(tmpRectF);
|
||||
float boxCornerRadiusBottomLeft =
|
||||
shapeAppearanceModel.getBottomLeftCornerSize().getCornerSize(tmpRectF);
|
||||
float boxCornerRadiusBottomRight =
|
||||
shapeAppearanceModel.getBottomRightCornerSize().getCornerSize(tmpRectF);
|
||||
setBoxCornerRadii(
|
||||
shouldCornersBeRtl ? boxCornerRadiusTopLeft : boxCornerRadiusTopRight,
|
||||
shouldCornersBeRtl ? boxCornerRadiusTopRight : boxCornerRadiusTopLeft,
|
||||
shouldCornersBeRtl ? boxCornerRadiusBottomLeft : boxCornerRadiusBottomRight,
|
||||
shouldCornersBeRtl ? boxCornerRadiusBottomRight : boxCornerRadiusBottomLeft
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
||||
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
|
||||
|
||||
@ -27,6 +27,7 @@ import static androidx.test.espresso.matcher.ViewMatchers.isEnabled;
|
||||
import static androidx.test.espresso.matcher.ViewMatchers.withId;
|
||||
import static androidx.test.espresso.matcher.ViewMatchers.withText;
|
||||
import static com.google.android.material.testutils.TestUtilsActions.setEnabled;
|
||||
import static com.google.android.material.testutils.TestUtilsActions.setLayoutDirection;
|
||||
import static com.google.android.material.testutils.TestUtilsMatchers.withTextColor;
|
||||
import static com.google.android.material.testutils.TestUtilsMatchers.withTypeface;
|
||||
import static com.google.android.material.testutils.TextInputLayoutActions.setBoxBackgroundColor;
|
||||
@ -64,6 +65,7 @@ import android.graphics.drawable.ColorDrawable;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Build;
|
||||
import android.os.Parcelable;
|
||||
import androidx.core.view.ViewCompat;
|
||||
import androidx.core.widget.TextViewCompat;
|
||||
import android.text.TextPaint;
|
||||
import android.util.AttributeSet;
|
||||
@ -711,6 +713,120 @@ public class TextInputLayoutTest {
|
||||
onView(withId(R.id.textinput_box_outline)).check(isBoxCornerRadiusBottomEnd(boxCornerRadiusBottomEnd));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetBoxCornerRadiiInRtl() {
|
||||
float boxCornerRadiusTopStart = 0.5f;
|
||||
float boxCornerRadiusTopEnd = 1f;
|
||||
float boxCornerRadiusBottomStart = 1.5f;
|
||||
float boxCornerRadiusBottomEnd = 2f;
|
||||
final Activity activity = activityTestRule.getActivity();
|
||||
final TextInputLayout textInputLayout =
|
||||
activity.findViewById(R.id.textinput_box_outline);
|
||||
|
||||
// Set to RTL.
|
||||
onView(withId(R.id.textinput_box_outline))
|
||||
.perform(setLayoutDirection(ViewCompat.LAYOUT_DIRECTION_RTL));
|
||||
|
||||
if (ViewCompat.getLayoutDirection(textInputLayout) == ViewCompat.LAYOUT_DIRECTION_RTL) {
|
||||
// Set the outline box's corner radii.
|
||||
onView(withId(R.id.textinput_box_outline))
|
||||
.perform(
|
||||
setBoxCornerRadii(
|
||||
boxCornerRadiusTopStart,
|
||||
boxCornerRadiusTopEnd,
|
||||
boxCornerRadiusBottomStart,
|
||||
boxCornerRadiusBottomEnd));
|
||||
|
||||
// Assert values match each respective corner in RTL.
|
||||
onView(withId(R.id.textinput_box_outline))
|
||||
.check(isBoxCornerRadiusTopLeft(boxCornerRadiusTopEnd));
|
||||
onView(withId(R.id.textinput_box_outline))
|
||||
.check(isBoxCornerRadiusTopRight(boxCornerRadiusTopStart));
|
||||
onView(withId(R.id.textinput_box_outline))
|
||||
.check(isBoxCornerRadiusBottomLeft(boxCornerRadiusBottomEnd));
|
||||
onView(withId(R.id.textinput_box_outline))
|
||||
.check(isBoxCornerRadiusBottomRight(boxCornerRadiusBottomStart));
|
||||
// Assert getCornerRadius methods return correct values.
|
||||
onView(withId(R.id.textinput_box_outline)).check(isBoxCornerRadiusTopStart(boxCornerRadiusTopStart));
|
||||
onView(withId(R.id.textinput_box_outline)).check(isBoxCornerRadiusTopEnd(boxCornerRadiusTopEnd));
|
||||
onView(withId(R.id.textinput_box_outline)).check(isBoxCornerRadiusBottomStart(boxCornerRadiusBottomStart));
|
||||
onView(withId(R.id.textinput_box_outline)).check(isBoxCornerRadiusBottomEnd(boxCornerRadiusBottomEnd));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetBoxCornerRadiiSwitchFromLtrToRtl() {
|
||||
float boxCornerRadiusTopStart = 0.5f;
|
||||
float boxCornerRadiusTopEnd = 1f;
|
||||
float boxCornerRadiusBottomStart = 1.5f;
|
||||
float boxCornerRadiusBottomEnd = 2f;
|
||||
final Activity activity = activityTestRule.getActivity();
|
||||
final TextInputLayout textInputLayout =
|
||||
activity.findViewById(R.id.textinput_box_outline);
|
||||
|
||||
// Set the outline box's corner radii.
|
||||
onView(withId(R.id.textinput_box_outline))
|
||||
.perform(
|
||||
setBoxCornerRadii(
|
||||
boxCornerRadiusTopStart,
|
||||
boxCornerRadiusTopEnd,
|
||||
boxCornerRadiusBottomStart,
|
||||
boxCornerRadiusBottomEnd));
|
||||
// Set to RTL.
|
||||
onView(withId(R.id.textinput_box_outline))
|
||||
.perform(setLayoutDirection(ViewCompat.LAYOUT_DIRECTION_RTL));
|
||||
|
||||
if (ViewCompat.getLayoutDirection(textInputLayout) == ViewCompat.LAYOUT_DIRECTION_RTL) {
|
||||
// Assert values match what they should be in RTL.
|
||||
onView(withId(R.id.textinput_box_outline))
|
||||
.check(isBoxCornerRadiusTopLeft(boxCornerRadiusTopEnd));
|
||||
onView(withId(R.id.textinput_box_outline))
|
||||
.check(isBoxCornerRadiusTopRight(boxCornerRadiusTopStart));
|
||||
onView(withId(R.id.textinput_box_outline))
|
||||
.check(isBoxCornerRadiusBottomLeft(boxCornerRadiusBottomEnd));
|
||||
onView(withId(R.id.textinput_box_outline))
|
||||
.check(isBoxCornerRadiusBottomRight(boxCornerRadiusBottomStart));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetBoxCornerRadiiSwitchFromRtlToLtr() {
|
||||
float boxCornerRadiusTopStart = 0.5f;
|
||||
float boxCornerRadiusTopEnd = 1f;
|
||||
float boxCornerRadiusBottomStart = 1.5f;
|
||||
float boxCornerRadiusBottomEnd = 2f;
|
||||
final Activity activity = activityTestRule.getActivity();
|
||||
final TextInputLayout textInputLayout =
|
||||
activity.findViewById(R.id.textinput_box_outline);
|
||||
// Set to RTL.
|
||||
onView(withId(R.id.textinput_box_outline))
|
||||
.perform(setLayoutDirection(ViewCompat.LAYOUT_DIRECTION_RTL));
|
||||
|
||||
// Set the outline box's corner radii.
|
||||
onView(withId(R.id.textinput_box_outline))
|
||||
.perform(
|
||||
setBoxCornerRadii(
|
||||
boxCornerRadiusTopStart,
|
||||
boxCornerRadiusTopEnd,
|
||||
boxCornerRadiusBottomStart,
|
||||
boxCornerRadiusBottomEnd));
|
||||
|
||||
if (ViewCompat.getLayoutDirection(textInputLayout) == ViewCompat.LAYOUT_DIRECTION_RTL) {
|
||||
// Set back to LTR.
|
||||
onView(withId(R.id.textinput_box_outline))
|
||||
.perform(setLayoutDirection(ViewCompat.LAYOUT_DIRECTION_LTR));
|
||||
// Assert values are correct.
|
||||
onView(withId(R.id.textinput_box_outline))
|
||||
.check(isBoxCornerRadiusTopLeft(boxCornerRadiusTopStart));
|
||||
onView(withId(R.id.textinput_box_outline))
|
||||
.check(isBoxCornerRadiusTopRight(boxCornerRadiusTopEnd));
|
||||
onView(withId(R.id.textinput_box_outline))
|
||||
.check(isBoxCornerRadiusBottomLeft(boxCornerRadiusBottomStart));
|
||||
onView(withId(R.id.textinput_box_outline))
|
||||
.check(isBoxCornerRadiusBottomRight(boxCornerRadiusBottomEnd));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBoxTopEndCornerRadiusChangesWithFloatValue() {
|
||||
float cornerRadiusSmall =
|
||||
@ -860,6 +976,46 @@ public class TextInputLayoutTest {
|
||||
};
|
||||
}
|
||||
|
||||
private static ViewAssertion isBoxCornerRadiusTopLeft(final float boxCornerRadius) {
|
||||
return (view, noViewFoundException) -> {
|
||||
assertThat(view).isInstanceOf(TextInputLayout.class);
|
||||
assertEquals(
|
||||
boxCornerRadius,
|
||||
((TextInputLayout) view).getBoxBackground().getTopLeftCornerResolvedSize(),
|
||||
0.01);
|
||||
};
|
||||
}
|
||||
|
||||
private static ViewAssertion isBoxCornerRadiusTopRight(final float boxCornerRadius) {
|
||||
return (view, noViewFoundException) -> {
|
||||
assertThat(view).isInstanceOf(TextInputLayout.class);
|
||||
assertEquals(
|
||||
boxCornerRadius,
|
||||
((TextInputLayout) view).getBoxBackground().getTopRightCornerResolvedSize(),
|
||||
0.01);
|
||||
};
|
||||
}
|
||||
|
||||
private static ViewAssertion isBoxCornerRadiusBottomLeft(final float boxCornerRadius) {
|
||||
return (view, noViewFoundException) -> {
|
||||
assertThat(view).isInstanceOf(TextInputLayout.class);
|
||||
assertEquals(
|
||||
boxCornerRadius,
|
||||
((TextInputLayout) view).getBoxBackground().getBottomLeftCornerResolvedSize(),
|
||||
0.01);
|
||||
};
|
||||
}
|
||||
|
||||
private static ViewAssertion isBoxCornerRadiusBottomRight(final float boxCornerRadius) {
|
||||
return (view, noViewFoundException) -> {
|
||||
assertThat(view).isInstanceOf(TextInputLayout.class);
|
||||
assertEquals(
|
||||
boxCornerRadius,
|
||||
((TextInputLayout) view).getBoxBackground().getBottomRightCornerResolvedSize(),
|
||||
0.01);
|
||||
};
|
||||
}
|
||||
|
||||
private static ViewAssertion isCutoutOpen(final boolean open) {
|
||||
return (view, noViewFoundException) -> {
|
||||
assertTrue(view instanceof TextInputLayout);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user