Skip to content

Commit

Permalink
[TextInputLayout] Add scale type setters/getters for start and end im…
Browse files Browse the repository at this point in the history
…age buttons in the TextInputLayout

Resolves #2080

PiperOrigin-RevId: 463381963
  • Loading branch information
imhappi authored and drchen committed Jul 28, 2022
1 parent 3daa028 commit e284e57
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/components/TextField.md
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,9 @@ Element | Attribute | Related method(s)
**Color** | `app:startIconTint` | `setStartIconTintList` | `?attr/colorOnSurfaceVariant` (see all [states](https://github.com/material-components/material-components-android/tree/master/lib/java/com/google/android/material/textfield/res/color/m3_textfield_indicator_text_color.xml))
**Checkable** | `app:startIconCheckable` | `setStartIconCheckable`<br/>`isStartIconCheckable` | `false`
**Size** | `app:startIconMinSize` | `setStartIconMinSize`<br/>`getStartIconMinSize` | `48dp`
**Scale type** | `app:startIconScaleType` | `setStartIconScaleType` <br/> `getStartIconScaleType` |

`ScaleType.CENTER`

#### Label attributes

Expand Down Expand Up @@ -511,6 +514,9 @@ Element | Attribute | Related
**Error icon** | `app:errorIconDrawable` | `setErrorIconDrawable`<br/>`getErrorIconDrawable` | [`@drawable/mtrl_ic_error`](https://github.com/material-components/material-components-android/tree/master/lib/java/com/google/android/material/textfield/res/drawable/mtrl_ic_error.xml)
**Error icon color** | `app:errorIconTint` | `setErrorIconTintList` | `?attr/colorError`
**Size** | `app:endIconMinSize` | `setEndIconMinSize`<br/>`getEndIconMinSize` | `48dp`
**Scale type** | `app:endIconScaleType` | `setEndIconScaleType` <br/> `getEndIconScaleType` |

`ScaleType.CENTER`

#### Activation indicator attributes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
import com.google.android.material.R;

import static com.google.android.material.textfield.IconHelper.applyIconTint;
import static com.google.android.material.textfield.IconHelper.convertScaleType;
import static com.google.android.material.textfield.IconHelper.refreshIconDrawableState;
import static com.google.android.material.textfield.IconHelper.setCompatRippleBackgroundIfNeeded;
import static com.google.android.material.textfield.IconHelper.setIconMinSize;
import static com.google.android.material.textfield.IconHelper.setIconOnClickListener;
import static com.google.android.material.textfield.IconHelper.setIconOnLongClickListener;
import static com.google.android.material.textfield.IconHelper.setIconScaleType;
import static com.google.android.material.textfield.TextInputLayout.END_ICON_CLEAR_TEXT;
import static com.google.android.material.textfield.TextInputLayout.END_ICON_CUSTOM;
import static com.google.android.material.textfield.TextInputLayout.END_ICON_DROPDOWN_MENU;
Expand All @@ -49,6 +51,7 @@
import android.view.accessibility.AccessibilityManager;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.DrawableRes;
Expand Down Expand Up @@ -96,6 +99,7 @@ class EndCompoundLayout extends LinearLayout {
private ColorStateList endIconTintList;
private PorterDuff.Mode endIconTintMode;
private int endIconMinSize;
@NonNull private ScaleType endIconScaleType;
private OnLongClickListener endIconOnLongClickListener;

@Nullable private CharSequence suffixText;
Expand Down Expand Up @@ -280,6 +284,10 @@ private void initEndIconView(TintTypedArray a) {
a.getDimensionPixelSize(
R.styleable.TextInputLayout_endIconMinSize,
getResources().getDimensionPixelSize(R.dimen.mtrl_min_touch_target_size)));
if (a.hasValue(R.styleable.TextInputLayout_endIconScaleType)) {
setEndIconScaleType(
convertScaleType(a.getInt(R.styleable.TextInputLayout_endIconScaleType, -1)));
}
}

private void initSuffixTextView(TintTypedArray a) {
Expand Down Expand Up @@ -567,6 +575,16 @@ int getEndIconMinSize() {
return endIconMinSize;
}

void setEndIconScaleType(@NonNull ScaleType endIconScaleType) {
this.endIconScaleType = endIconScaleType;
setIconScaleType(endIconView, endIconScaleType);
setIconScaleType(errorIconView, endIconScaleType);
}

@NonNull ScaleType getEndIconScaleType() {
return endIconScaleType;
}

void addOnEndIconChangedListener(@NonNull OnEndIconChangedListener listener) {
endIconChangedListeners.add(listener);
}
Expand Down
25 changes: 25 additions & 0 deletions lib/java/com/google/android/material/textfield/IconHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import android.os.Build.VERSION_CODES;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.Px;
Expand Down Expand Up @@ -150,4 +151,28 @@ static void setIconMinSize(@NonNull CheckableImageButton iconView, @Px int iconS
iconView.setMinimumWidth(iconSize);
iconView.setMinimumHeight(iconSize);
}

static void setIconScaleType(
@NonNull CheckableImageButton iconView, @NonNull ImageView.ScaleType scaleType) {
iconView.setScaleType(scaleType);
}

static ImageView.ScaleType convertScaleType(int scaleType) {
switch (scaleType) {
case 0:
return ImageView.ScaleType.FIT_XY;
case 1:
return ImageView.ScaleType.FIT_START;
case 2:
return ImageView.ScaleType.FIT_CENTER;
case 3:
return ImageView.ScaleType.FIT_END;
case 5:
return ImageView.ScaleType.CENTER_CROP;
case 6:
return ImageView.ScaleType.CENTER_INSIDE;
default:
return ImageView.ScaleType.CENTER;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
import com.google.android.material.R;

import static com.google.android.material.textfield.IconHelper.applyIconTint;
import static com.google.android.material.textfield.IconHelper.convertScaleType;
import static com.google.android.material.textfield.IconHelper.refreshIconDrawableState;
import static com.google.android.material.textfield.IconHelper.setCompatRippleBackgroundIfNeeded;
import static com.google.android.material.textfield.IconHelper.setIconMinSize;
import static com.google.android.material.textfield.IconHelper.setIconOnClickListener;
import static com.google.android.material.textfield.IconHelper.setIconOnLongClickListener;
import static com.google.android.material.textfield.IconHelper.setIconScaleType;

import android.annotation.SuppressLint;
import android.content.res.ColorStateList;
Expand All @@ -38,6 +40,7 @@
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
Expand Down Expand Up @@ -68,6 +71,7 @@ class StartCompoundLayout extends LinearLayout {
private ColorStateList startIconTintList;
private PorterDuff.Mode startIconTintMode;
private int startIconMinSize;
@NonNull private ScaleType startIconScaleType;
private OnLongClickListener startIconOnLongClickListener;

private boolean hintExpanded;
Expand Down Expand Up @@ -134,6 +138,10 @@ private void initStartIconView(TintTypedArray a) {
a.getDimensionPixelSize(
R.styleable.TextInputLayout_startIconMinSize,
getResources().getDimensionPixelSize(R.dimen.mtrl_min_touch_target_size)));
if (a.hasValue(R.styleable.TextInputLayout_startIconScaleType)) {
setStartIconScaleType(
convertScaleType(a.getInt(R.styleable.TextInputLayout_startIconScaleType, -1)));
}
}

private void initPrefixTextView(TintTypedArray a) {
Expand Down Expand Up @@ -285,6 +293,16 @@ int getStartIconMinSize() {
return startIconMinSize;
}

void setStartIconScaleType(@NonNull ScaleType startIconScaleType) {
this.startIconScaleType = startIconScaleType;
setIconScaleType(startIconView, startIconScaleType);
}

@NonNull
ScaleType getStartIconScaleType() {
return startIconScaleType;
}

void setupAccessibilityNodeInfo(@NonNull AccessibilityNodeInfoCompat info) {
if (prefixTextView.getVisibility() == VISIBLE) {
info.setLabelFor(prefixTextView);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.ColorInt;
Expand Down Expand Up @@ -3503,6 +3504,52 @@ public int getEndIconMinSize() {
return endLayout.getEndIconMinSize();
}

/**
* Sets {@link ImageView.ScaleType} for the start icon's ImageButton.
*
* @param scaleType {@link ImageView.ScaleType} for the start icon's ImageButton.
* @attr ref android.support.design.button.R.styleable#TextInputLayout_startIconScaleType
* @see #getStartIconScaleType()
*/
public void setStartIconScaleType(@NonNull ScaleType scaleType) {
startLayout.setStartIconScaleType(scaleType);
}

/**
* Returns the {@link ImageView.ScaleType} for the start icon's ImageButton.
*
* @return Returns the {@link ImageView.ScaleType} for the start icon's ImageButton.
* @attr ref android.support.design.button.R.styleable#TextInputLayout_startIconScaleType
* @see #setStartIconScaleType(ScaleType)
*/
@NonNull
public ScaleType getStartIconScaleType() {
return startLayout.getStartIconScaleType();
}

/**
* Sets {@link ImageView.ScaleType} for the end icon's ImageButton.
*
* @param scaleType {@link ImageView.ScaleType} for the end icon's ImageButton.
* @attr ref android.support.design.button.R.styleable#TextInputLayout_endIconScaleType
* @see #getEndIconScaleType()
*/
public void setEndIconScaleType(@NonNull ScaleType scaleType) {
endLayout.setEndIconScaleType(scaleType);
}

/**
* Returns the {@link ImageView.ScaleType} for the end icon's ImageButton.
*
* @return Returns the {@link ImageView.ScaleType} for the end icon's ImageButton.
* @attr ref android.support.design.button.R.styleable#TextInputLayout_endIconScaleType
* @see #setEndIconScaleType(ScaleType)
*/
@NonNull
public ScaleType getEndIconScaleType() {
return endLayout.getEndIconScaleType();
}

/**
* Set a content description for the end icon.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
<public name="startIconTint" type="attr"/>
<public name="startIconTintMode" type="attr"/>
<public name="startIconMinSize" type="attr"/>
<public name="startIconScaleType" type="attr" />
<public name="placeholderText" type="attr"/>
<public name="placeholderTextAppearance" type="attr"/>
<public name="placeholderTextColor" type="attr"/>
Expand All @@ -81,6 +82,7 @@
<public name="endIconTint" type="attr"/>
<public name="endIconTintMode" type="attr"/>
<public name="endIconMinSize" type="attr"/>
<public name="endIconScaleType" type="attr" />
<!-- Deprecated. Use endIconMode instead. -->
<public name="passwordToggleEnabled" type="attr"/>
<!--Deprecated. Use endIconDrawable instead. -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,28 @@
</attr>
<!-- The minimum size of the start icon. -->
<attr name="startIconMinSize" format="dimension"/>
<!-- The scale type of the start icon. -->
<attr name="startIconScaleType">
<!-- Scale the image using {@link android.graphics.Matrix.ScaleToFit#FILL}. -->
<enum name="fitXY" value="0" />
<!-- Scale the image using {@link android.graphics.Matrix.ScaleToFit#START}. -->
<enum name="fitStart" value="1" />
<!-- Scale the image using {@link android.graphics.Matrix.ScaleToFit#CENTER}. -->
<enum name="fitCenter" value="2" />
<!-- Scale the image using {@link android.graphics.Matrix.ScaleToFit#END}. -->
<enum name="fitEnd" value="3" />
<!-- Center the image in the view, but perform no scaling. -->
<enum name="center" value="4" />
<!-- Scale the image uniformly (maintain the image's aspect ratio) so both dimensions
(width and height) of the image will be equal to or larger than the corresponding
dimension of the view (minus padding). The image is then centered in the view. -->
<enum name="centerCrop" value="5" />
<!-- Scale the image uniformly (maintain the image's aspect ratio) so that both
dimensions (width and height) of the image will be equal to or less than the
corresponding dimension of the view (minus padding). The image is then centered in
the view. -->
<enum name="centerInside" value="6" />
</attr>
<!-- The end icon mode of the TextInputLayout. It will display one of the end icons detailed
below, or no end icon. -->
<attr name="endIconMode">
Expand Down Expand Up @@ -220,6 +242,28 @@
</attr>
<!-- The minimum size of the end icon -->
<attr name="endIconMinSize" format="dimension"/>
<!-- The scale type of the end icon. -->
<attr name="endIconScaleType">
<!-- Scale the image using {@link android.graphics.Matrix.ScaleToFit#FILL}. -->
<enum name="fitXY" value="0" />
<!-- Scale the image using {@link android.graphics.Matrix.ScaleToFit#START}. -->
<enum name="fitStart" value="1" />
<!-- Scale the image using {@link android.graphics.Matrix.ScaleToFit#CENTER}. -->
<enum name="fitCenter" value="2" />
<!-- Scale the image using {@link android.graphics.Matrix.ScaleToFit#END}. -->
<enum name="fitEnd" value="3" />
<!-- Center the image in the view, but perform no scaling. -->
<enum name="center" value="4" />
<!-- Scale the image uniformly (maintain the image's aspect ratio) so both dimensions
(width and height) of the image will be equal to or larger than the corresponding
dimension of the view (minus padding). The image is then centered in the view. -->
<enum name="centerCrop" value="5" />
<!-- Scale the image uniformly (maintain the image's aspect ratio) so that both
dimensions (width and height) of the image will be equal to or less than the
corresponding dimension of the view (minus padding). The image is then centered in
the view. -->
<enum name="centerInside" value="6" />
</attr>

<!-- Whether the text input area should be drawn as a filled box, an outline box, or not as a box.-->
<attr name="boxBackgroundMode">
Expand Down

0 comments on commit e284e57

Please sign in to comment.