Skip to content

Commit

Permalink
Add support for placeholder text in TextInputLayout.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
afohrman authored and dsn5ft committed Dec 11, 2019
1 parent fe160f3 commit 201ec62
Show file tree
Hide file tree
Showing 19 changed files with 352 additions and 20 deletions.
Expand Up @@ -84,6 +84,16 @@ public void initTextFieldDemoControls(LayoutInflater layoutInflater, View view)
String.valueOf(helperTextTextField.getEditText().getText()));
}
});
// Initialize button for updating the placeholder text.
TextInputLayout placeholderTextField = view.findViewById(R.id.text_input_placeholder);
view.findViewById(R.id.button_update_placeholder)
.setOnClickListener(
v -> {
if (!checkTextInputIsNull(placeholderTextField)) {
setAllTextFieldsPlaceholder(
String.valueOf(placeholderTextField.getEditText().getText()));
}
});

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

private void setAllTextFieldsPlaceholder(String placeholder) {
for (TextInputLayout textfield : textfields) {
textfield.setPlaceholderText(placeholder);
}
}

private void setAllTextFieldsCounterMax(int length) {
for (TextInputLayout textfield : textfields) {
textfield.setCounterMaxLength(length);
Expand Down
Expand Up @@ -33,7 +33,8 @@
android:layout_margin="4dp"
android:hint="@string/cat_textfield_label"
app:helperText="@string/cat_textfield_outlined_helper_text"
app:helperTextEnabled="true">
app:helperTextEnabled="true"
app:placeholderText="@string/cat_textfield_placeholder_text">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Expand All @@ -46,7 +47,8 @@
android:layout_margin="4dp"
android:hint="@string/cat_textfield_label"
app:helperText="@string/cat_textfield_outlined_dense_helper_text"
app:helperTextEnabled="true">
app:helperTextEnabled="true"
app:placeholderText="@string/cat_textfield_placeholder_text">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Expand All @@ -59,7 +61,8 @@
android:layout_margin="4dp"
android:hint="@string/cat_textfield_label"
app:helperText="@string/cat_textfield_filled_helper_text"
app:helperTextEnabled="true">
app:helperTextEnabled="true"
app:placeholderText="@string/cat_textfield_placeholder_text">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Expand All @@ -72,7 +75,8 @@
android:layout_margin="4dp"
android:hint="@string/cat_textfield_label"
app:helperText="@string/cat_textfield_filled_dense_helper_text"
app:helperTextEnabled="true">
app:helperTextEnabled="true"
app:placeholderText="@string/cat_textfield_placeholder_text">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Expand Down
Expand Up @@ -130,6 +130,24 @@
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/cat_textfield_update_counter_text"/>

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/text_input_placeholder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:hint="@string/cat_textfield_customize_placeholder_text"
app:errorEnabled="true">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.google.android.material.textfield.TextInputLayout>
<Button
android:id="@+id/button_update_placeholder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/cat_textfield_update_placeholder_text"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
Expand Up @@ -31,7 +31,8 @@
app:counterMaxLength="10"
app:errorEnabled="true"
app:helperText="@string/cat_textfield_filled_helper_text"
app:helperTextEnabled="true">
app:helperTextEnabled="true"
app:placeholderText="@string/cat_textfield_placeholder_text">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Expand All @@ -47,7 +48,8 @@
app:counterMaxLength="10"
app:errorEnabled="true"
app:helperText="@string/cat_textfield_filled_dense_helper_text"
app:helperTextEnabled="true">
app:helperTextEnabled="true"
app:placeholderText="@string/cat_textfield_placeholder_text">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Expand Down
Expand Up @@ -31,7 +31,8 @@
app:endIconMode="password_toggle"
app:errorEnabled="true"
app:helperText="@string/cat_textfield_filled_dense_password_helper_text"
app:helperTextEnabled="true">
app:helperTextEnabled="true"
app:placeholderText="@string/cat_textfield_placeholder_text">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand All @@ -47,7 +48,8 @@
app:endIconMode="clear_text"
app:errorEnabled="true"
app:helperText="@string/cat_textfield_filled_clear_button_helper_text"
app:helperTextEnabled="true">
app:helperTextEnabled="true"
app:placeholderText="@string/cat_textfield_placeholder_text">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Expand All @@ -64,7 +66,8 @@
app:endIconMode="custom"
app:errorEnabled="true"
app:helperText="@string/cat_textfield_filled_custom_end_icon_helper_text"
app:helperTextEnabled="true">
app:helperTextEnabled="true"
app:placeholderText="@string/cat_textfield_placeholder_text">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Expand All @@ -82,7 +85,8 @@
app:helperText="@string/cat_textfield_filled_start_icon_helper_text"
app:helperTextEnabled="true"
app:startIconContentDescription="@string/cat_textfield_custom_start_icon_content_description"
app:startIconDrawable="@drawable/ic_search_24px">
app:startIconDrawable="@drawable/ic_search_24px"
app:placeholderText="@string/cat_textfield_placeholder_text">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Expand All @@ -103,7 +107,8 @@
app:helperText="@string/cat_textfield_filled_start_end_icons_helper_text"
app:helperTextEnabled="true"
app:startIconContentDescription="@string/cat_textfield_custom_start_icon_content_description"
app:startIconDrawable="@drawable/ic_search_24px">
app:startIconDrawable="@drawable/ic_search_24px"
app:placeholderText="@string/cat_textfield_placeholder_text">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Expand Down
Expand Up @@ -31,7 +31,8 @@
app:counterMaxLength="10"
app:errorEnabled="true"
app:helperText="@string/cat_textfield_legacy_helper_text"
app:helperTextEnabled="true">
app:helperTextEnabled="true"
app:placeholderText="@string/cat_textfield_placeholder_text">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Expand Down
Expand Up @@ -32,7 +32,8 @@
app:counterMaxLength="10"
app:errorEnabled="true"
app:helperText="@string/cat_textfield_outlined_helper_text"
app:helperTextEnabled="true">
app:helperTextEnabled="true"
app:placeholderText="@string/cat_textfield_placeholder_text">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Expand All @@ -48,7 +49,8 @@
app:counterMaxLength="10"
app:errorEnabled="true"
app:helperText="@string/cat_textfield_outlined_dense_helper_text"
app:helperTextEnabled="true">
app:helperTextEnabled="true"
app:placeholderText="@string/cat_textfield_placeholder_text">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Expand Down
Expand Up @@ -31,7 +31,8 @@
app:endIconMode="password_toggle"
app:errorEnabled="true"
app:helperText="@string/cat_textfield_outlined_dense_password_helper_text"
app:helperTextEnabled="true">
app:helperTextEnabled="true"
app:placeholderText="@string/cat_textfield_placeholder_text">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand All @@ -47,7 +48,8 @@
app:endIconMode="clear_text"
app:errorEnabled="true"
app:helperText="@string/cat_textfield_outlined_clear_button_helper_text"
app:helperTextEnabled="true">
app:helperTextEnabled="true"
app:placeholderText="@string/cat_textfield_placeholder_text">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Expand All @@ -64,7 +66,8 @@
app:endIconMode="custom"
app:errorEnabled="true"
app:helperText="@string/cat_textfield_outlined_custom_end_icon_helper_text"
app:helperTextEnabled="true">
app:helperTextEnabled="true"
app:placeholderText="@string/cat_textfield_placeholder_text">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Expand All @@ -81,6 +84,7 @@
app:errorEnabled="true"
app:helperText="@string/cat_textfield_outlined_start_icon_helper_text"
app:helperTextEnabled="true"
app:placeholderText="@string/cat_textfield_placeholder_text"
app:startIconContentDescription="@string/cat_textfield_custom_start_icon_content_description"
app:startIconDrawable="@drawable/ic_search_24px">
<com.google.android.material.textfield.TextInputEditText
Expand All @@ -102,6 +106,7 @@
app:errorEnabled="true"
app:helperText="@string/cat_textfield_outlined_start_end_icons_helper_text"
app:helperTextEnabled="true"
app:placeholderText="@string/cat_textfield_placeholder_text"
app:startIconContentDescription="@string/cat_textfield_custom_start_icon_content_description"
app:startIconDrawable="@drawable/ic_search_24px">
<com.google.android.material.textfield.TextInputEditText
Expand Down
Expand Up @@ -73,15 +73,21 @@
<string name="cat_textfield_label" translatable="false">Label</string>
<string name="cat_textfield_password" translatable="false">Password</string>
<string name="cat_textfield_error" translatable="false">Error</string>
<string name="cat_textfield_placeholder_text" translatable="false">Placeholder text</string>

<string name="cat_textfield_customize_color" translatable="false">Change color</string>
<string name="cat_textfield_customize_counter_text" translatable="false">Character counter max</string>
<string name="cat_textfield_customize_label_text" translatable="false">Label text</string>
<string name="cat_textfield_customize_error_text" translatable="false">Error text</string>
<string name="cat_textfield_customize_helper_text" translatable="false">Helper text</string>
<string name="cat_textfield_customize_color" translatable="false">Change color</string>
<string name="cat_textfield_customize_counter_text" translatable="false">Character counter max</string>
<string name="cat_textfield_customize_placeholder_text" translatable="false">Placeholder text</string>

<string name="cat_textfield_update_label_text" translatable="false">Update label</string>
<string name="cat_textfield_update_error_text" translatable="false">Update error</string>
<string name="cat_textfield_update_helper_text" translatable="false">Update helper</string>
<string name="cat_textfield_update_counter_text" translatable="false">Update character counter</string>
<string name="cat_textfield_update_placeholder_text" translatable="false">Update placeholder</string>

<string name="cat_textfield_null_input_error_text" translatable="false">Enter a value</string>
<string name="cat_textfield_show_error_text" translatable="false">Show error</string>
<string name="cat_textfield_hide_error_text" translatable="false">Hide error</string>
Expand Down
1 change: 1 addition & 0 deletions docs/components/TextInputLayout.md
Expand Up @@ -150,6 +150,7 @@ Label (also called a “Floating Label”) | [`androi
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\))
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\))
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)
Placeholder text | [`app:placeholderText`](https://developer.android.com/reference/com/google/android/material/textfield/TextInputLayout#attr_TextInputLayout_placeholderText)
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)
Prefix text | [`app:prefixText`](https://developer.android.com/reference/com/google/android/material/textfield/TextInputLayout#attr_TextInputLayout_prefixText)
Suffix text | [`app:suffixText`](https://developer.android.com/reference/com/google/android/material/textfield/TextInputLayout#attr_TextInputLayout_suffixText)
Expand Down

0 comments on commit 201ec62

Please sign in to comment.