Skip to content

Commit

Permalink
[TextInputLayout] Added method to set corner family programmatically …
Browse files Browse the repository at this point in the history
…and a setShapeAppearanceModel method for greater customizability of the box background shape.

Resolves #1957

PiperOrigin-RevId: 470826160
  • Loading branch information
leticiarossi committed Aug 30, 2022
1 parent b8e1b57 commit 08c40d8
Show file tree
Hide file tree
Showing 3 changed files with 313 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@
import com.google.android.material.internal.ViewUtils;
import com.google.android.material.motion.MotionUtils;
import com.google.android.material.resources.MaterialResources;
import com.google.android.material.shape.CornerFamily;
import com.google.android.material.shape.CornerTreatment;
import com.google.android.material.shape.MaterialShapeDrawable;
import com.google.android.material.shape.ShapeAppearanceModel;
import java.lang.annotation.Retention;
Expand Down Expand Up @@ -1220,6 +1222,46 @@ public int getBoxBackgroundColor() {
return boxBackgroundColor;
}

/**
* Sets the {@link ShapeAppearanceModel} of the text field's box background.
*
* @param shapeAppearanceModel the desired shape appearance model.
* @see #getShapeAppearanceModel()
*/
public void setShapeAppearanceModel(@NonNull ShapeAppearanceModel shapeAppearanceModel) {
if (boxBackground != null && boxBackground.getShapeAppearanceModel() != shapeAppearanceModel) {
this.shapeAppearanceModel = shapeAppearanceModel;
applyBoxAttributes();
}
}

/**
* Returns the {@link ShapeAppearanceModel} of the text field's box background.
*
* @see #setShapeAppearanceModel(ShapeAppearanceModel)
*/
@NonNull
public ShapeAppearanceModel getShapeAppearanceModel() {
return shapeAppearanceModel;
}

/**
* Sets the box's corner family for all corners of the text field.
*
* @param cornerFamily the {@link CornerFamily} to be used. May be one of {@link
* CornerFamily#ROUNDED} or {@link CornerFamily#CUT}.
*/
public void setBoxCornerFamily(@CornerFamily int cornerFamily) {
shapeAppearanceModel =
shapeAppearanceModel.toBuilder()
.setTopLeftCorner(cornerFamily, shapeAppearanceModel.getTopLeftCornerSize())
.setTopRightCorner(cornerFamily, shapeAppearanceModel.getTopRightCornerSize())
.setBottomLeftCorner(cornerFamily, shapeAppearanceModel.getBottomLeftCornerSize())
.setBottomRightCorner(cornerFamily, shapeAppearanceModel.getBottomRightCornerSize())
.build();
applyBoxAttributes();
}

/**
* Set the resources used for the box's corner radii.
*
Expand Down Expand Up @@ -3040,9 +3082,8 @@ public void setExpandedHintEnabled(boolean enabled) {
public void onRtlPropertiesChanged(int layoutDirection) {
super.onRtlPropertiesChanged(layoutDirection);
boolean isLayoutDirectionRtl = layoutDirection == ViewCompat.LAYOUT_DIRECTION_RTL;
if (isLayoutDirectionRtl != areCornerRadiiRtl) {
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 =
Expand All @@ -3051,12 +3092,29 @@ public void onRtlPropertiesChanged(int layoutDirection) {
shapeAppearanceModel.getBottomLeftCornerSize().getCornerSize(tmpRectF);
float boxCornerRadiusBottomRight =
shapeAppearanceModel.getBottomRightCornerSize().getCornerSize(tmpRectF);
setBoxCornerRadii(
shouldCornersBeRtl ? boxCornerRadiusTopLeft : boxCornerRadiusTopRight,
shouldCornersBeRtl ? boxCornerRadiusTopRight : boxCornerRadiusTopLeft,
shouldCornersBeRtl ? boxCornerRadiusBottomLeft : boxCornerRadiusBottomRight,
shouldCornersBeRtl ? boxCornerRadiusBottomRight : boxCornerRadiusBottomLeft);
}
CornerTreatment topLeftTreatment =
shapeAppearanceModel.getTopLeftCorner();
CornerTreatment topRightTreatment =
shapeAppearanceModel.getTopRightCorner();
CornerTreatment bottomLeftTreatment =
shapeAppearanceModel.getBottomLeftCorner();
CornerTreatment bottomRightTreatment =
shapeAppearanceModel.getBottomRightCorner();

ShapeAppearanceModel newShapeAppearanceModel =
ShapeAppearanceModel.builder()
.setTopLeftCorner(topRightTreatment)
.setTopRightCorner(topLeftTreatment)
.setBottomLeftCorner(bottomRightTreatment)
.setBottomRightCorner(bottomLeftTreatment)
.setTopLeftCornerSize(boxCornerRadiusTopRight)
.setTopRightCornerSize(boxCornerRadiusTopLeft)
.setBottomLeftCornerSize(boxCornerRadiusBottomRight)
.setBottomRightCornerSize(boxCornerRadiusBottomLeft)
.build();
areCornerRadiiRtl = isLayoutDirectionRtl;
setShapeAppearanceModel(newShapeAppearanceModel);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@
import android.widget.AutoCompleteTextView;
import androidx.annotation.ColorInt;
import androidx.annotation.DimenRes;
import androidx.annotation.NonNull;
import androidx.test.espresso.UiController;
import androidx.test.espresso.ViewAction;
import androidx.test.espresso.matcher.ViewMatchers;
import com.google.android.material.internal.CheckableImageButton;
import com.google.android.material.shape.CornerFamily;
import com.google.android.material.shape.ShapeAppearanceModel;
import com.google.android.material.testapp.R;
import com.google.android.material.textfield.TextInputLayout;
import org.hamcrest.Matcher;
Expand Down Expand Up @@ -639,6 +642,49 @@ public void perform(UiController uiController, View view) {
};
}

/** Sets the {@link ShapeAppearanceModel} of the text field's box background. */
public static ViewAction setShapeAppearanceModel(
@NonNull ShapeAppearanceModel shapeAppearanceModel) {
return new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return isAssignableFrom(TextInputLayout.class);
}

@Override
public String getDescription() {
return "Sets the box's shape appearance";
}

@Override
public void perform(UiController uiController, View view) {
TextInputLayout layout = (TextInputLayout) view;
layout.setShapeAppearanceModel(shapeAppearanceModel);
}
};
}

/** Sets the corner family for all corners of the text field. */
public static ViewAction setBoxCornerFamily(@CornerFamily final int cornerFamily) {
return new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return isAssignableFrom(TextInputLayout.class);
}

@Override
public String getDescription() {
return "Sets the box's corner family";
}

@Override
public void perform(UiController uiController, View view) {
TextInputLayout layout = (TextInputLayout) view;
layout.setBoxCornerFamily(cornerFamily);
}
};
}

public static ViewAction setBoxCornerRadii(
final float topLeftCornerRadius,
final float topRightCornerRadius,
Expand Down
Loading

0 comments on commit 08c40d8

Please sign in to comment.