Skip to content

Commit 201ec62

Browse files
afohrmandsn5ft
authored andcommitted
Add support for placeholder text in TextInputLayout.
This is the initial implementation of placeholder text; it does not contain motion. At this stage, TextInputLayout sets the placeholder text visibility based on whether or not it should show. PiperOrigin-RevId: 284808331
1 parent fe160f3 commit 201ec62

19 files changed

Lines changed: 352 additions & 20 deletions

File tree

catalog/java/io/material/catalog/textfield/TextFieldControllableDemoFragment.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,16 @@ public void initTextFieldDemoControls(LayoutInflater layoutInflater, View view)
8484
String.valueOf(helperTextTextField.getEditText().getText()));
8585
}
8686
});
87+
// Initialize button for updating the placeholder text.
88+
TextInputLayout placeholderTextField = view.findViewById(R.id.text_input_placeholder);
89+
view.findViewById(R.id.button_update_placeholder)
90+
.setOnClickListener(
91+
v -> {
92+
if (!checkTextInputIsNull(placeholderTextField)) {
93+
setAllTextFieldsPlaceholder(
94+
String.valueOf(placeholderTextField.getEditText().getText()));
95+
}
96+
});
8797

8898
// Initialize button for updating the counter max.
8999
TextInputLayout counterMaxTextField = view.findViewById(R.id.text_input_counter_max);
@@ -131,6 +141,12 @@ private void setAllTextFieldsHelperText(String helperText) {
131141
}
132142
}
133143

144+
private void setAllTextFieldsPlaceholder(String placeholder) {
145+
for (TextInputLayout textfield : textfields) {
146+
textfield.setPlaceholderText(placeholder);
147+
}
148+
}
149+
134150
private void setAllTextFieldsCounterMax(int length) {
135151
for (TextInputLayout textfield : textfields) {
136152
textfield.setCounterMaxLength(length);

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
android:layout_margin="4dp"
3434
android:hint="@string/cat_textfield_label"
3535
app:helperText="@string/cat_textfield_outlined_helper_text"
36-
app:helperTextEnabled="true">
36+
app:helperTextEnabled="true"
37+
app:placeholderText="@string/cat_textfield_placeholder_text">
3738
<com.google.android.material.textfield.TextInputEditText
3839
android:layout_width="match_parent"
3940
android:layout_height="wrap_content"/>
@@ -46,7 +47,8 @@
4647
android:layout_margin="4dp"
4748
android:hint="@string/cat_textfield_label"
4849
app:helperText="@string/cat_textfield_outlined_dense_helper_text"
49-
app:helperTextEnabled="true">
50+
app:helperTextEnabled="true"
51+
app:placeholderText="@string/cat_textfield_placeholder_text">
5052
<com.google.android.material.textfield.TextInputEditText
5153
android:layout_width="match_parent"
5254
android:layout_height="wrap_content"/>
@@ -59,7 +61,8 @@
5961
android:layout_margin="4dp"
6062
android:hint="@string/cat_textfield_label"
6163
app:helperText="@string/cat_textfield_filled_helper_text"
62-
app:helperTextEnabled="true">
64+
app:helperTextEnabled="true"
65+
app:placeholderText="@string/cat_textfield_placeholder_text">
6366
<com.google.android.material.textfield.TextInputEditText
6467
android:layout_width="match_parent"
6568
android:layout_height="wrap_content"/>
@@ -72,7 +75,8 @@
7275
android:layout_margin="4dp"
7376
android:hint="@string/cat_textfield_label"
7477
app:helperText="@string/cat_textfield_filled_dense_helper_text"
75-
app:helperTextEnabled="true">
78+
app:helperTextEnabled="true"
79+
app:placeholderText="@string/cat_textfield_placeholder_text">
7680
<com.google.android.material.textfield.TextInputEditText
7781
android:layout_width="match_parent"
7882
android:layout_height="wrap_content"/>

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,24 @@
130130
android:layout_height="wrap_content"
131131
android:layout_gravity="center_horizontal"
132132
android:text="@string/cat_textfield_update_counter_text"/>
133+
134+
<com.google.android.material.textfield.TextInputLayout
135+
android:id="@+id/text_input_placeholder"
136+
android:layout_width="match_parent"
137+
android:layout_height="wrap_content"
138+
android:layout_margin="4dp"
139+
android:hint="@string/cat_textfield_customize_placeholder_text"
140+
app:errorEnabled="true">
141+
<com.google.android.material.textfield.TextInputEditText
142+
android:layout_width="match_parent"
143+
android:layout_height="wrap_content"/>
144+
</com.google.android.material.textfield.TextInputLayout>
145+
<Button
146+
android:id="@+id/button_update_placeholder"
147+
android:layout_width="wrap_content"
148+
android:layout_height="wrap_content"
149+
android:layout_gravity="center_horizontal"
150+
android:text="@string/cat_textfield_update_placeholder_text"/>
133151
</LinearLayout>
134152
</LinearLayout>
135153
</ScrollView>

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
app:counterMaxLength="10"
3232
app:errorEnabled="true"
3333
app:helperText="@string/cat_textfield_filled_helper_text"
34-
app:helperTextEnabled="true">
34+
app:helperTextEnabled="true"
35+
app:placeholderText="@string/cat_textfield_placeholder_text">
3536
<com.google.android.material.textfield.TextInputEditText
3637
android:layout_width="match_parent"
3738
android:layout_height="wrap_content"/>
@@ -47,7 +48,8 @@
4748
app:counterMaxLength="10"
4849
app:errorEnabled="true"
4950
app:helperText="@string/cat_textfield_filled_dense_helper_text"
50-
app:helperTextEnabled="true">
51+
app:helperTextEnabled="true"
52+
app:placeholderText="@string/cat_textfield_placeholder_text">
5153
<com.google.android.material.textfield.TextInputEditText
5254
android:layout_width="match_parent"
5355
android:layout_height="wrap_content"/>

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
app:endIconMode="password_toggle"
3232
app:errorEnabled="true"
3333
app:helperText="@string/cat_textfield_filled_dense_password_helper_text"
34-
app:helperTextEnabled="true">
34+
app:helperTextEnabled="true"
35+
app:placeholderText="@string/cat_textfield_placeholder_text">
3536
<com.google.android.material.textfield.TextInputEditText
3637
android:layout_width="match_parent"
3738
android:layout_height="wrap_content"
@@ -47,7 +48,8 @@
4748
app:endIconMode="clear_text"
4849
app:errorEnabled="true"
4950
app:helperText="@string/cat_textfield_filled_clear_button_helper_text"
50-
app:helperTextEnabled="true">
51+
app:helperTextEnabled="true"
52+
app:placeholderText="@string/cat_textfield_placeholder_text">
5153
<com.google.android.material.textfield.TextInputEditText
5254
android:layout_width="match_parent"
5355
android:layout_height="wrap_content"/>
@@ -64,7 +66,8 @@
6466
app:endIconMode="custom"
6567
app:errorEnabled="true"
6668
app:helperText="@string/cat_textfield_filled_custom_end_icon_helper_text"
67-
app:helperTextEnabled="true">
69+
app:helperTextEnabled="true"
70+
app:placeholderText="@string/cat_textfield_placeholder_text">
6871
<com.google.android.material.textfield.TextInputEditText
6972
android:layout_width="match_parent"
7073
android:layout_height="wrap_content"/>
@@ -82,7 +85,8 @@
8285
app:helperText="@string/cat_textfield_filled_start_icon_helper_text"
8386
app:helperTextEnabled="true"
8487
app:startIconContentDescription="@string/cat_textfield_custom_start_icon_content_description"
85-
app:startIconDrawable="@drawable/ic_search_24px">
88+
app:startIconDrawable="@drawable/ic_search_24px"
89+
app:placeholderText="@string/cat_textfield_placeholder_text">
8690
<com.google.android.material.textfield.TextInputEditText
8791
android:layout_width="match_parent"
8892
android:layout_height="wrap_content"/>
@@ -103,7 +107,8 @@
103107
app:helperText="@string/cat_textfield_filled_start_end_icons_helper_text"
104108
app:helperTextEnabled="true"
105109
app:startIconContentDescription="@string/cat_textfield_custom_start_icon_content_description"
106-
app:startIconDrawable="@drawable/ic_search_24px">
110+
app:startIconDrawable="@drawable/ic_search_24px"
111+
app:placeholderText="@string/cat_textfield_placeholder_text">
107112
<com.google.android.material.textfield.TextInputEditText
108113
android:layout_width="match_parent"
109114
android:layout_height="wrap_content"/>

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
app:counterMaxLength="10"
3232
app:errorEnabled="true"
3333
app:helperText="@string/cat_textfield_legacy_helper_text"
34-
app:helperTextEnabled="true">
34+
app:helperTextEnabled="true"
35+
app:placeholderText="@string/cat_textfield_placeholder_text">
3536
<com.google.android.material.textfield.TextInputEditText
3637
android:layout_width="match_parent"
3738
android:layout_height="wrap_content"/>

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
app:counterMaxLength="10"
3333
app:errorEnabled="true"
3434
app:helperText="@string/cat_textfield_outlined_helper_text"
35-
app:helperTextEnabled="true">
35+
app:helperTextEnabled="true"
36+
app:placeholderText="@string/cat_textfield_placeholder_text">
3637
<com.google.android.material.textfield.TextInputEditText
3738
android:layout_width="match_parent"
3839
android:layout_height="wrap_content"/>
@@ -48,7 +49,8 @@
4849
app:counterMaxLength="10"
4950
app:errorEnabled="true"
5051
app:helperText="@string/cat_textfield_outlined_dense_helper_text"
51-
app:helperTextEnabled="true">
52+
app:helperTextEnabled="true"
53+
app:placeholderText="@string/cat_textfield_placeholder_text">
5254
<com.google.android.material.textfield.TextInputEditText
5355
android:layout_width="match_parent"
5456
android:layout_height="wrap_content"/>

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
app:endIconMode="password_toggle"
3232
app:errorEnabled="true"
3333
app:helperText="@string/cat_textfield_outlined_dense_password_helper_text"
34-
app:helperTextEnabled="true">
34+
app:helperTextEnabled="true"
35+
app:placeholderText="@string/cat_textfield_placeholder_text">
3536
<com.google.android.material.textfield.TextInputEditText
3637
android:layout_width="match_parent"
3738
android:layout_height="wrap_content"
@@ -47,7 +48,8 @@
4748
app:endIconMode="clear_text"
4849
app:errorEnabled="true"
4950
app:helperText="@string/cat_textfield_outlined_clear_button_helper_text"
50-
app:helperTextEnabled="true">
51+
app:helperTextEnabled="true"
52+
app:placeholderText="@string/cat_textfield_placeholder_text">
5153
<com.google.android.material.textfield.TextInputEditText
5254
android:layout_width="match_parent"
5355
android:layout_height="wrap_content"/>
@@ -64,7 +66,8 @@
6466
app:endIconMode="custom"
6567
app:errorEnabled="true"
6668
app:helperText="@string/cat_textfield_outlined_custom_end_icon_helper_text"
67-
app:helperTextEnabled="true">
69+
app:helperTextEnabled="true"
70+
app:placeholderText="@string/cat_textfield_placeholder_text">
6871
<com.google.android.material.textfield.TextInputEditText
6972
android:layout_width="match_parent"
7073
android:layout_height="wrap_content"/>
@@ -81,6 +84,7 @@
8184
app:errorEnabled="true"
8285
app:helperText="@string/cat_textfield_outlined_start_icon_helper_text"
8386
app:helperTextEnabled="true"
87+
app:placeholderText="@string/cat_textfield_placeholder_text"
8488
app:startIconContentDescription="@string/cat_textfield_custom_start_icon_content_description"
8589
app:startIconDrawable="@drawable/ic_search_24px">
8690
<com.google.android.material.textfield.TextInputEditText
@@ -102,6 +106,7 @@
102106
app:errorEnabled="true"
103107
app:helperText="@string/cat_textfield_outlined_start_end_icons_helper_text"
104108
app:helperTextEnabled="true"
109+
app:placeholderText="@string/cat_textfield_placeholder_text"
105110
app:startIconContentDescription="@string/cat_textfield_custom_start_icon_content_description"
106111
app:startIconDrawable="@drawable/ic_search_24px">
107112
<com.google.android.material.textfield.TextInputEditText

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,21 @@
7373
<string name="cat_textfield_label" translatable="false">Label</string>
7474
<string name="cat_textfield_password" translatable="false">Password</string>
7575
<string name="cat_textfield_error" translatable="false">Error</string>
76+
<string name="cat_textfield_placeholder_text" translatable="false">Placeholder text</string>
77+
78+
<string name="cat_textfield_customize_color" translatable="false">Change color</string>
79+
<string name="cat_textfield_customize_counter_text" translatable="false">Character counter max</string>
7680
<string name="cat_textfield_customize_label_text" translatable="false">Label text</string>
7781
<string name="cat_textfield_customize_error_text" translatable="false">Error text</string>
7882
<string name="cat_textfield_customize_helper_text" translatable="false">Helper text</string>
79-
<string name="cat_textfield_customize_color" translatable="false">Change color</string>
80-
<string name="cat_textfield_customize_counter_text" translatable="false">Character counter max</string>
83+
<string name="cat_textfield_customize_placeholder_text" translatable="false">Placeholder text</string>
84+
8185
<string name="cat_textfield_update_label_text" translatable="false">Update label</string>
8286
<string name="cat_textfield_update_error_text" translatable="false">Update error</string>
8387
<string name="cat_textfield_update_helper_text" translatable="false">Update helper</string>
8488
<string name="cat_textfield_update_counter_text" translatable="false">Update character counter</string>
89+
<string name="cat_textfield_update_placeholder_text" translatable="false">Update placeholder</string>
90+
8591
<string name="cat_textfield_null_input_error_text" translatable="false">Enter a value</string>
8692
<string name="cat_textfield_show_error_text" translatable="false">Show error</string>
8793
<string name="cat_textfield_hide_error_text" translatable="false">Hide error</string>

docs/components/TextInputLayout.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ Label (also called a “Floating Label”) | [`androi
150150
Error message | [`app:errorEnabled`](https://developer.android.com/reference/com/google/android/material/textfield/TextInputLayout#attr_TextInputLayout_errorEnabled)<br/>[`#setError(CharSequence)`](https://developer.android.com/reference/com/google/android/material/textfield/TextInputLayout#setError\(java.lang.CharSequence\))
151151
Error icon | [`app:errorIconDrawable`](https://developer.android.com/reference/com/google/android/material/textfield/TextInputLayout#attr_TextInputLayout_errorIconDrawable)<br/>[`#setErrorIconDrawable(Drawable)`](https://developer.android.com/reference/com/google/android/material/textfield/TextInputLayout#setErrorIconDrawable\(android.graphics.drawable.Drawable\))
152152
Helper text | [`app:helperTextEnabled`](https://developer.android.com/reference/com/google/android/material/textfield/TextInputLayout#attr_TextInputLayout_helperTextEnabled)<br/>[`app:helperText`](https://developer.android.com/reference/com/google/android/material/textfield/TextInputLayout#attr_TextInputLayout_helperText)
153+
Placeholder text | [`app:placeholderText`](https://developer.android.com/reference/com/google/android/material/textfield/TextInputLayout#attr_TextInputLayout_placeholderText)
153154
Character counter | [`app:counterEnabled`](https://developer.android.com/reference/com/google/android/material/textfield/TextInputLayout#attr_TextInputLayout_counterEnabled)<br/>[`app:counterMaxLength`](https://developer.android.com/reference/com/google/android/material/textfield/TextInputLayout#attr_TextInputLayout_counterMaxLength)
154155
Prefix text | [`app:prefixText`](https://developer.android.com/reference/com/google/android/material/textfield/TextInputLayout#attr_TextInputLayout_prefixText)
155156
Suffix text | [`app:suffixText`](https://developer.android.com/reference/com/google/android/material/textfield/TextInputLayout#attr_TextInputLayout_suffixText)

0 commit comments

Comments
 (0)