Skip to content

Commit 2084fd3

Browse files
leticiarossidsn5ft
authored andcommitted
[Text Fields] Pan window so that keyboard doesn't hide helper/error views.
It doesn't pan if the user can already scroll (for example if it's in a scroll view). PiperOrigin-RevId: 286424421
1 parent a320475 commit 2084fd3

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,9 @@
1515
~ limitations under the License.
1616
-->
1717

18-
<ScrollView
18+
<LinearLayout
1919
xmlns:android="http://schemas.android.com/apk/res/android"
2020
xmlns:app="http://schemas.android.com/apk/res-auto"
21-
android:layout_width="match_parent"
22-
android:layout_height="wrap_content">
23-
<LinearLayout
2421
android:layout_width="match_parent"
2522
android:layout_height="wrap_content"
2623
android:layout_margin="@dimen/cat_textfield_standard_spacing"
@@ -82,4 +79,3 @@
8279
android:layout_height="wrap_content"/>
8380
</com.google.android.material.textfield.TextInputLayout>
8481
</LinearLayout>
85-
</ScrollView>

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import com.google.android.material.R;
2020

2121
import android.content.Context;
22+
import android.graphics.Point;
23+
import android.graphics.Rect;
2224
import android.os.Build;
2325
import android.os.Build.VERSION;
2426
import androidx.annotation.NonNull;
@@ -41,6 +43,8 @@
4143
*/
4244
public class TextInputEditText extends AppCompatEditText {
4345

46+
private final Rect parentRect = new Rect();
47+
4448
public TextInputEditText(Context context) {
4549
this(context, null);
4650
}
@@ -111,6 +115,43 @@ private CharSequence getHintFromLayout() {
111115
return (layout != null) ? layout.getHint() : null;
112116
}
113117

118+
@Override
119+
public void getFocusedRect(@Nullable Rect r) {
120+
super.getFocusedRect(r);
121+
TextInputLayout textInputLayout = getTextInputLayout();
122+
if (textInputLayout != null && r != null) {
123+
textInputLayout.getFocusedRect(parentRect);
124+
r.bottom = parentRect.bottom;
125+
}
126+
}
127+
128+
@Override
129+
public boolean getGlobalVisibleRect(@Nullable Rect r, @Nullable Point globalOffset) {
130+
boolean result = super.getGlobalVisibleRect(r, globalOffset);
131+
TextInputLayout textInputLayout = getTextInputLayout();
132+
if (textInputLayout != null && r != null) {
133+
textInputLayout.getGlobalVisibleRect(parentRect, globalOffset);
134+
r.bottom = parentRect.bottom;
135+
}
136+
return result;
137+
}
138+
139+
@Override
140+
public boolean requestRectangleOnScreen(@Nullable Rect rectangle) {
141+
boolean result = super.requestRectangleOnScreen(rectangle);
142+
TextInputLayout textInputLayout = getTextInputLayout();
143+
if (textInputLayout != null) {
144+
parentRect.set(
145+
0,
146+
textInputLayout.getHeight()
147+
- getResources().getDimensionPixelOffset(R.dimen.mtrl_edittext_rectangle_top_offset),
148+
textInputLayout.getWidth(),
149+
textInputLayout.getHeight());
150+
textInputLayout.requestRectangleOnScreen(parentRect, true);
151+
}
152+
return result;
153+
}
154+
114155
@Override
115156
public void onInitializeAccessibilityNodeInfo(@NonNull AccessibilityNodeInfo info) {
116157
super.onInitializeAccessibilityNodeInfo(info);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
<dimen name="mtrl_textinput_box_stroke_width_focused">2dp</dimen>
2929
<dimen name="mtrl_textinput_box_label_cutout_padding">4dp</dimen>
3030

31+
<dimen name="mtrl_edittext_rectangle_top_offset">12dp</dimen>
32+
3133
<!-- The icon views' minimum width is 48dp and since each icon is 24dp wide we need a 4dp margin
3234
to have the appropriate spacing before and after each icon. -->
3335
<dimen name="mtrl_textinput_end_icon_margin_start">4dp</dimen>

0 commit comments

Comments
 (0)