Skip to content

Commit 5685941

Browse files
leticiarossiafohrman
authored andcommitted
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
1 parent ba719ba commit 5685941

File tree

5 files changed

+63
-1
lines changed

5 files changed

+63
-1
lines changed

catalog/java/io/material/catalog/textfield/res/layout/cat_textfield_filled_content.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,23 @@
8585
android:layout_height="wrap_content"/>
8686
</com.google.android.material.textfield.TextInputLayout>
8787

88+
<com.google.android.material.textfield.TextInputLayout
89+
style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox"
90+
android:layout_width="match_parent"
91+
android:layout_height="wrap_content"
92+
android:layout_margin="4dp"
93+
android:hint="@string/cat_textfield_label"
94+
app:errorEnabled="true"
95+
app:helperText="@string/cat_textfield_filled_custom_end_icon_helper_text"
96+
app:helperTextEnabled="true"
97+
app:endIconMode="custom"
98+
app:endIconDrawable="@drawable/ic_accelerator_24px"
99+
app:endIconContentDescription="@string/cat_textfield_custom_end_icon_content_description">
100+
<com.google.android.material.textfield.TextInputEditText
101+
android:layout_width="match_parent"
102+
android:layout_height="wrap_content"/>
103+
</com.google.android.material.textfield.TextInputLayout>
104+
88105
<com.google.android.material.textfield.TextInputLayout
89106
android:id="@+id/text_input_demo_box_filled_start_icon"
90107
style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox"

catalog/java/io/material/catalog/textfield/res/layout/cat_textfield_outline_content.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,23 @@
7272
android:layout_height="wrap_content"/>
7373
</com.google.android.material.textfield.TextInputLayout>
7474

75+
<com.google.android.material.textfield.TextInputLayout
76+
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
77+
android:layout_width="match_parent"
78+
android:layout_height="wrap_content"
79+
android:layout_margin="4dp"
80+
android:hint="@string/cat_textfield_label"
81+
app:errorEnabled="true"
82+
app:helperText="@string/cat_textfield_outline_custom_end_icon_helper_text"
83+
app:helperTextEnabled="true"
84+
app:endIconMode="custom"
85+
app:endIconDrawable="@drawable/ic_accelerator_24px"
86+
app:endIconContentDescription="@string/cat_textfield_custom_end_icon_content_description">
87+
<com.google.android.material.textfield.TextInputEditText
88+
android:layout_width="match_parent"
89+
android:layout_height="wrap_content"/>
90+
</com.google.android.material.textfield.TextInputLayout>
91+
7592
<com.google.android.material.textfield.TextInputLayout
7693
android:id="@+id/text_input_demo_box_outline_start_icon"
7794
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"

catalog/java/io/material/catalog/textfield/res/values/strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,13 @@
3232
</string>
3333
<string name="cat_textfield_filled_start_icon_helper_text">Filled start icon text field</string>
3434
<string name="cat_textfield_filled_clear_button_helper_text">Filled clear button text field</string>
35+
<string name="cat_textfield_filled_custom_end_icon_helper_text">Filled custom end icon text field</string>
3536
<string name="cat_textfield_outline_helper_text">Outline text field</string>
3637
<string name="cat_textfield_outline_dense_helper_text">Dense outline text field</string>
3738
<string name="cat_textfield_outline_start_icon_helper_text">Outline start icon text field</string>
3839
<string name="cat_textfield_outline_clear_button_helper_text">Outline clear button text field</string>
40+
<string name="cat_textfield_outline_custom_end_icon_helper_text">Outline custom end icon text field</string>
41+
<string name="cat_textfield_custom_end_icon_content_description">Custom end icon</string>
3942

4043
<string name="cat_textfield_customize_section_title">Customize text fields</string>
4144
<string name="cat_textfield_customize_section_description">

lib/java/com/google/android/material/textfield/TextInputLayout.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@
106106
* disguised, when your EditText is set to display a password.
107107
* <li>Clearing text functionality via {@link #setEndIconMode(int)} API and related attribute. If
108108
* set, a button is displayed when text is present and clicking it clears the EditText field.
109+
* <li>Showing a custom icon specified by the user via {@link #setEndIconMode(int)} API and
110+
* related attribute. The user should specify a drawable and content description for the icon.
111+
* Optionally, the user can also specify an {@link android.view.View.OnClickListener}, an
112+
* {@link OnEndIconInitializedListener} and an {@link OnEndIconChangedListener}.
109113
* <p><strong>Note:</strong> When using an end icon, the 'end' compound drawable of the
110114
* EditText will be overridden while the end icon view is visible. To ensure that any existing
111115
* drawables are restored correctly, you should set those compound drawables relatively
@@ -217,10 +221,23 @@ public class TextInputLayout extends LinearLayout {
217221
private Typeface typeface;
218222

219223
/** Values for the end icon mode. */
220-
@IntDef({END_ICON_NONE, END_ICON_PASSWORD_TOGGLE, END_ICON_CLEAR_TEXT})
224+
@IntDef({END_ICON_CUSTOM, END_ICON_NONE, END_ICON_PASSWORD_TOGGLE, END_ICON_CLEAR_TEXT})
221225
@Retention(RetentionPolicy.SOURCE)
222226
public @interface EndIconMode {}
223227

228+
/**
229+
* The TextInputLayout will show a custom icon specified by the user.
230+
*
231+
* @see #setEndIconMode(int)
232+
* @see #getEndIconMode()
233+
* @see #setEndIconDrawable(Drawable)
234+
* @see #setEndIconContentDescription(CharSequence)
235+
* @see #setEndIconOnClickListener(OnClickListener) (optionally)
236+
* @see #addOnEndIconInitializedListener(OnEndIconInitializedListener) (optionally)
237+
* @see #addOnEndIconChangedListener(OnEndIconChangedListener) (optionally)
238+
*/
239+
public static final int END_ICON_CUSTOM = -1;
240+
224241
/**
225242
* Default for the TextInputLayout. It will not display an end icon.
226243
*
@@ -1978,6 +1995,10 @@ public void setEndIconMode(@EndIconMode int endIconMode) {
19781995
.inflate(R.layout.design_text_input_end_icon, inputFrame, false);
19791996
}
19801997
switch (endIconMode) {
1998+
case END_ICON_CUSTOM:
1999+
setEndIconOnClickListener(null);
2000+
setEndIconVisible(true);
2001+
break;
19812002
case END_ICON_PASSWORD_TOGGLE:
19822003
// Set defaults for the password toggle end icon
19832004
setEndIconPasswordToggleDefaults();
@@ -2021,6 +2042,8 @@ public int getEndIconMode() {
20212042
public void setEndIconOnClickListener(@Nullable OnClickListener onClickListener) {
20222043
if (endIconView != null) {
20232044
endIconView.setOnClickListener(onClickListener);
2045+
endIconView.setFocusable(onClickListener != null);
2046+
endIconView.setClickable(onClickListener != null);
20242047
}
20252048
}
20262049

lib/java/com/google/android/material/textfield/res/values/attrs.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@
7171
<!-- The end icon mode of the TextInputLayout. It will display one of the end icons detailed
7272
below, or no end icon. -->
7373
<attr name="endIconMode">
74+
<!-- The view will display a custom icon specified by the user. -->
75+
<enum name="custom" value="-1"/>
7476
<!-- No end icon. -->
7577
<enum name="none" value="0"/>
7678
<!-- The view will display a toggle when the EditText has a password. -->

0 commit comments

Comments
 (0)