From 56859415e88224b9fa039f641eaaf7953a699e5d Mon Sep 17 00:00:00 2001 From: leticiars Date: Tue, 19 Feb 2019 17:09:13 -0500 Subject: [PATCH] Adding support for a custom end icon for the TextInputLayout. A user-specified icon can now be set via app:endIconMode="custom" and attributes app:endIconDrawable and app:endIconContentDescription. It is also possible to set a custom OnClickListener via TextInputLayout's end icon API. PiperOrigin-RevId: 234671237 --- .../layout/cat_textfield_filled_content.xml | 17 +++++++++++++ .../layout/cat_textfield_outline_content.xml | 17 +++++++++++++ .../catalog/textfield/res/values/strings.xml | 3 +++ .../material/textfield/TextInputLayout.java | 25 ++++++++++++++++++- .../material/textfield/res/values/attrs.xml | 2 ++ 5 files changed, 63 insertions(+), 1 deletion(-) diff --git a/catalog/java/io/material/catalog/textfield/res/layout/cat_textfield_filled_content.xml b/catalog/java/io/material/catalog/textfield/res/layout/cat_textfield_filled_content.xml index df51b9f90fb..d65d0f05ef7 100644 --- a/catalog/java/io/material/catalog/textfield/res/layout/cat_textfield_filled_content.xml +++ b/catalog/java/io/material/catalog/textfield/res/layout/cat_textfield_filled_content.xml @@ -85,6 +85,23 @@ android:layout_height="wrap_content"/> + + + + + + + + Filled start icon text field Filled clear button text field + Filled custom end icon text field Outline text field Dense outline text field Outline start icon text field Outline clear button text field + Outline custom end icon text field + Custom end icon Customize text fields diff --git a/lib/java/com/google/android/material/textfield/TextInputLayout.java b/lib/java/com/google/android/material/textfield/TextInputLayout.java index 6f1567492af..970d2fd4125 100644 --- a/lib/java/com/google/android/material/textfield/TextInputLayout.java +++ b/lib/java/com/google/android/material/textfield/TextInputLayout.java @@ -106,6 +106,10 @@ * disguised, when your EditText is set to display a password. *
  • Clearing text functionality via {@link #setEndIconMode(int)} API and related attribute. If * set, a button is displayed when text is present and clicking it clears the EditText field. + *
  • Showing a custom icon specified by the user via {@link #setEndIconMode(int)} API and + * related attribute. The user should specify a drawable and content description for the icon. + * Optionally, the user can also specify an {@link android.view.View.OnClickListener}, an + * {@link OnEndIconInitializedListener} and an {@link OnEndIconChangedListener}. *

    Note: When using an end icon, the 'end' compound drawable of the * EditText will be overridden while the end icon view is visible. To ensure that any existing * drawables are restored correctly, you should set those compound drawables relatively @@ -217,10 +221,23 @@ public class TextInputLayout extends LinearLayout { private Typeface typeface; /** Values for the end icon mode. */ - @IntDef({END_ICON_NONE, END_ICON_PASSWORD_TOGGLE, END_ICON_CLEAR_TEXT}) + @IntDef({END_ICON_CUSTOM, END_ICON_NONE, END_ICON_PASSWORD_TOGGLE, END_ICON_CLEAR_TEXT}) @Retention(RetentionPolicy.SOURCE) public @interface EndIconMode {} + /** + * The TextInputLayout will show a custom icon specified by the user. + * + * @see #setEndIconMode(int) + * @see #getEndIconMode() + * @see #setEndIconDrawable(Drawable) + * @see #setEndIconContentDescription(CharSequence) + * @see #setEndIconOnClickListener(OnClickListener) (optionally) + * @see #addOnEndIconInitializedListener(OnEndIconInitializedListener) (optionally) + * @see #addOnEndIconChangedListener(OnEndIconChangedListener) (optionally) + */ + public static final int END_ICON_CUSTOM = -1; + /** * Default for the TextInputLayout. It will not display an end icon. * @@ -1978,6 +1995,10 @@ public void setEndIconMode(@EndIconMode int endIconMode) { .inflate(R.layout.design_text_input_end_icon, inputFrame, false); } switch (endIconMode) { + case END_ICON_CUSTOM: + setEndIconOnClickListener(null); + setEndIconVisible(true); + break; case END_ICON_PASSWORD_TOGGLE: // Set defaults for the password toggle end icon setEndIconPasswordToggleDefaults(); @@ -2021,6 +2042,8 @@ public int getEndIconMode() { public void setEndIconOnClickListener(@Nullable OnClickListener onClickListener) { if (endIconView != null) { endIconView.setOnClickListener(onClickListener); + endIconView.setFocusable(onClickListener != null); + endIconView.setClickable(onClickListener != null); } } diff --git a/lib/java/com/google/android/material/textfield/res/values/attrs.xml b/lib/java/com/google/android/material/textfield/res/values/attrs.xml index f55737f59ba..a7faaa91cf0 100644 --- a/lib/java/com/google/android/material/textfield/res/values/attrs.xml +++ b/lib/java/com/google/android/material/textfield/res/values/attrs.xml @@ -71,6 +71,8 @@ + +