From a8d8e70d898e61a190f0fc64eae298a713fbbdb8 Mon Sep 17 00:00:00 2001 From: leticiars Date: Thu, 17 Mar 2022 18:33:36 +0000 Subject: [PATCH] [TextInputLayout][a11y] Fixed text field's focused rect wrong behavior when using a11y magnification. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before, in requestRectangleOnScreen we were setting the text input layout’s rectangle to a specific and constant position, which caused the zoomed in screen to be fixed to the left instead of following the input text as it was typed. We should use the edit text’s rectangle’s position first, then adjust for the text field’s height (which includes the helper/error text), then use that updated rectangle on the super call of the method. PiperOrigin-RevId: 435402032 (cherry picked from commit dce4419744ba4e336e31a80521ab8a9ecb1136de) --- .../material/textfield/TextInputEditText.java | 37 +++++++++---------- .../material/textfield/res/values/dimens.xml | 2 - 2 files changed, 17 insertions(+), 22 deletions(-) diff --git a/lib/java/com/google/android/material/textfield/TextInputEditText.java b/lib/java/com/google/android/material/textfield/TextInputEditText.java index e20c8eb7e7b..3827b7dcdd3 100644 --- a/lib/java/com/google/android/material/textfield/TextInputEditText.java +++ b/lib/java/com/google/android/material/textfield/TextInputEditText.java @@ -152,13 +152,15 @@ public boolean isTextInputLayoutFocusedRectEnabled() { return textInputLayoutFocusedRectEnabled; } + private boolean shouldUseTextInputLayoutFocusedRect(@Nullable TextInputLayout textInputLayout) { + return textInputLayout != null && textInputLayoutFocusedRectEnabled; + } + @Override public void getFocusedRect(@Nullable Rect r) { super.getFocusedRect(r); TextInputLayout textInputLayout = getTextInputLayout(); - if (textInputLayout != null - && textInputLayoutFocusedRectEnabled - && r != null) { + if (shouldUseTextInputLayoutFocusedRect(textInputLayout) && r != null) { textInputLayout.getFocusedRect(parentRect); r.bottom = parentRect.bottom; } @@ -166,31 +168,26 @@ public void getFocusedRect(@Nullable Rect r) { @Override public boolean getGlobalVisibleRect(@Nullable Rect r, @Nullable Point globalOffset) { - boolean result = super.getGlobalVisibleRect(r, globalOffset); TextInputLayout textInputLayout = getTextInputLayout(); - if (textInputLayout != null - && textInputLayoutFocusedRectEnabled - && r != null) { - textInputLayout.getGlobalVisibleRect(parentRect, globalOffset); - r.bottom = parentRect.bottom; - } - return result; + return shouldUseTextInputLayoutFocusedRect(textInputLayout) + ? textInputLayout.getGlobalVisibleRect(r, globalOffset) + : super.getGlobalVisibleRect(r, globalOffset); } @Override public boolean requestRectangleOnScreen(@Nullable Rect rectangle) { - boolean result = super.requestRectangleOnScreen(rectangle); TextInputLayout textInputLayout = getTextInputLayout(); - if (textInputLayout != null && textInputLayoutFocusedRectEnabled) { + if (shouldUseTextInputLayoutFocusedRect(textInputLayout) && rectangle != null) { + int bottomOffset = textInputLayout.getHeight() - getHeight(); parentRect.set( - 0, - textInputLayout.getHeight() - - getResources().getDimensionPixelOffset(R.dimen.mtrl_edittext_rectangle_top_offset), - textInputLayout.getWidth(), - textInputLayout.getHeight()); - textInputLayout.requestRectangleOnScreen(parentRect, true); + rectangle.left, + rectangle.top, + rectangle.right, + rectangle.bottom + bottomOffset); + return super.requestRectangleOnScreen(parentRect); + } else { + return super.requestRectangleOnScreen(rectangle); } - return result; } @Override diff --git a/lib/java/com/google/android/material/textfield/res/values/dimens.xml b/lib/java/com/google/android/material/textfield/res/values/dimens.xml index fdfc3f51f81..fd90094d77f 100644 --- a/lib/java/com/google/android/material/textfield/res/values/dimens.xml +++ b/lib/java/com/google/android/material/textfield/res/values/dimens.xml @@ -34,8 +34,6 @@ 2dp 4dp - 12dp - 23dp 12dp