diff --git a/docs/components/TextField.md b/docs/components/TextField.md index 1536d7a38..d97922de0 100644 --- a/docs/components/TextField.md +++ b/docs/components/TextField.md @@ -425,12 +425,14 @@ be set on the `TextInputEditText`)._ #### Label attributes -  | Attribute | Related method(s) | Default value ------------------------------- | ------------------------ | ------------------------------------------------------- | ------------- -**Text** | `android:hint` | `setHint`
`getHint` | `null` -**Color** | `android:textColorHint` | `setDefaultHintTextColor`
`getDefaultHintTextColor` | `?attr/colorOnSurface` at 60% opacity (see all [states](https://github.com/material-components/material-components-android/tree/master/lib/java/com/google/android/material/textfield/res/color/mtrl_indicator_text_color.xml)) -**Collapsed (floating) color** | `app:hintTextColor` | `setHintTextColor`
`getHintTextColor` | `?attr/colorPrimary` -**Typography** | `app:hintTextAppearance` | `setHintTextAppearance` | `?attr/textAppearanceCaption` +  | Attribute | Related method(s) | Default value +------------------------------ | -------------------------- | ------------------------------------------------------- | ------------- +**Text** | `android:hint` | `setHint`
`getHint` | `null` +**Color** | `android:textColorHint` | `setDefaultHintTextColor`
`getDefaultHintTextColor` | `?attr/colorOnSurface` at 60% opacity (see all [states](https://github.com/material-components/material-components-android/tree/master/lib/java/com/google/android/material/textfield/res/color/mtrl_indicator_text_color.xml)) +**Collapsed (floating) color** | `app:hintTextColor` | `setHintTextColor`
`getHintTextColor` | `?attr/colorPrimary` +**Typography** | `app:hintTextAppearance` | `setHintTextAppearance` | `?attr/textAppearanceCaption` +**Animation** | `app:hintAnimationEnabled` | `setHintAnimationEnabled`
`isHintAnimationEnabled` | `true` +**Expanded enabled** | `app:expandedHintEnabled` | `setExpandedHintEnabled`
`isExpandedHintEnabled` | `true` **Note:** The `android:hint` should always be set on the `TextInputLayout` instead of on the `EditText` in order to avoid unintended behaviors. diff --git a/lib/java/com/google/android/material/textfield/TextInputLayout.java b/lib/java/com/google/android/material/textfield/TextInputLayout.java index 3ea1b479f..6c9a9c0c5 100644 --- a/lib/java/com/google/android/material/textfield/TextInputLayout.java +++ b/lib/java/com/google/android/material/textfield/TextInputLayout.java @@ -406,6 +406,7 @@ public class TextInputLayout extends LinearLayout { final CollapsingTextHelper collapsingTextHelper = new CollapsingTextHelper(this); + private boolean expandedHintEnabled; private boolean hintAnimationEnabled; private ValueAnimator animator; @@ -474,6 +475,7 @@ public class TextInputLayout extends LinearLayout { hintEnabled = a.getBoolean(R.styleable.TextInputLayout_hintEnabled, true); setHint(a.getText(R.styleable.TextInputLayout_android_hint)); hintAnimationEnabled = a.getBoolean(R.styleable.TextInputLayout_hintAnimationEnabled, true); + expandedHintEnabled = a.getBoolean(R.styleable.TextInputLayout_expandedHintEnabled, true); shapeAppearanceModel = ShapeAppearanceModel.builder(context, attrs, defStyleAttr, DEF_STYLE_RES).build(); @@ -1516,7 +1518,7 @@ public class TextInputLayout extends LinearLayout { collapsingTextHelper.setCollapsedTextColor(focusedTextColor); } // If none of these states apply, leave the expanded and collapsed colors as they are. - if (hasText || (isEnabled() && hasFocus)) { + if (hasText || !expandedHintEnabled || (isEnabled() && hasFocus)) { // We should be showing the label so do so if it isn't already if (force || hintExpanded) { collapseHint(animate); @@ -2877,6 +2879,31 @@ public class TextInputLayout extends LinearLayout { hintAnimationEnabled = enabled; } + /** + * Returns whether the hint expands to occupy the input area when the text field is unpopulated + * and not focused. + * + * @see #setExpandedHintEnabled(boolean) + * @attr ref com.google.android.material.R.styleable#TextInputLayout_hintExpadedEnabled + */ + public boolean isExpandedHintEnabled() { + return expandedHintEnabled; + } + + /** + * Sets whether the hint should expand to occupy the input area when the text field is + * unpopulated and not focused. + * + * @see #isExpandedHintEnabled() + * @attr ref com.google.android.material.R.styleable#TextInputLayout_hintExpadedEnabled + */ + public void setExpandedHintEnabled(boolean enabled) { + if (expandedHintEnabled != enabled) { + expandedHintEnabled = enabled; + updateLabelState(false); + } + } + @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); diff --git a/lib/java/com/google/android/material/textfield/res-public/values/public.xml b/lib/java/com/google/android/material/textfield/res-public/values/public.xml index 4e41654c7..c460f5c35 100644 --- a/lib/java/com/google/android/material/textfield/res-public/values/public.xml +++ b/lib/java/com/google/android/material/textfield/res-public/values/public.xml @@ -62,6 +62,7 @@ + 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 833f772e0..20f42b1ac 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 @@ -37,6 +37,9 @@ + + diff --git a/tests/javatests/com/google/android/material/testutils/TextInputLayoutActions.java b/tests/javatests/com/google/android/material/testutils/TextInputLayoutActions.java index ca4f2b1d6..53335fa2e 100644 --- a/tests/javatests/com/google/android/material/testutils/TextInputLayoutActions.java +++ b/tests/javatests/com/google/android/material/testutils/TextInputLayoutActions.java @@ -827,4 +827,26 @@ public class TextInputLayoutActions { } }; } + + /** Sets whether the hint expands. */ + public static ViewAction setExpandedHintEnabled(final boolean expandedHintEnabled) { + return new ViewAction() { + + @Override + public Matcher getConstraints() { + return ViewMatchers.isAssignableFrom(TextInputLayout.class); + } + + @Override + public String getDescription() { + return "Sets whether the hint expands."; + } + + @Override + public void perform(UiController uiController, View view) { + TextInputLayout layout = (TextInputLayout) view; + layout.setExpandedHintEnabled(expandedHintEnabled); + } + }; + } } diff --git a/tests/javatests/com/google/android/material/textfield/TextInputLayoutTest.java b/tests/javatests/com/google/android/material/textfield/TextInputLayoutTest.java index c8c26f19b..7f0b4f9a0 100644 --- a/tests/javatests/com/google/android/material/textfield/TextInputLayoutTest.java +++ b/tests/javatests/com/google/android/material/textfield/TextInputLayoutTest.java @@ -41,6 +41,7 @@ import static com.google.android.material.testutils.TextInputLayoutActions.setEr import static com.google.android.material.testutils.TextInputLayoutActions.setErrorContentDescription; import static com.google.android.material.testutils.TextInputLayoutActions.setErrorEnabled; import static com.google.android.material.testutils.TextInputLayoutActions.setErrorTextAppearance; +import static com.google.android.material.testutils.TextInputLayoutActions.setExpandedHintEnabled; import static com.google.android.material.testutils.TextInputLayoutActions.setHelperText; import static com.google.android.material.testutils.TextInputLayoutActions.setHelperTextEnabled; import static com.google.android.material.testutils.TextInputLayoutActions.setHint; @@ -176,6 +177,15 @@ public class TextInputLayoutTest { onView(withId(R.id.textinput_box_outline)).check(isCutoutOpen(false)); } + @Test + public void testHintIsCollapsedIfExpandedHintNotEnabled() { + // Set expandedHintEnabled to false. + onView(withId(R.id.textinput_box_filled)).perform(setExpandedHintEnabled(false)); + + // Check the hint is collapsed. + onView(withId(R.id.textinput_box_filled)).check(isHintExpanded(false)); + } + @Test public void testSetErrorEnablesErrorIsDisplayed() { onView(withId(R.id.textinput)).perform(setError(ERROR_MESSAGE_1));