Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android doesn't resize my layout when there is a NestedScrollView in the hierarchy

I just observed the following behavior on my Nexus 9 with Android 7.1.1 and Android Support Library 25.3.1.

Here is my activity's layout:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusableInTouchMode="false"
        android:orientation="vertical"
        android:paddingBottom="4dp">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <View
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:background="#fffaaa" />

        <View
            android:id="@+id/view"
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:background="#bbbaaa" />

        <View
            android:layout_width="match_parent"
            android:layout_height="10dp"
            android:background="#f00" />

    </LinearLayout>
</ScrollView>

This is how it looks on the screen:

enter image description here

When the on-screen keyboard shows up, the system resizes my activity's layout so it takes up the space from the top of the screen to the keyboard. Please consider the dashed red line in the screenshot below:

enter image description here

However, when I replace the ScrollView with a NestedScrollView, the system doesn't resize the layout:

enter image description here

Now the dashed red line is below the keyboard. The issue can easily be fixed by applying android:windowSoftInputMode="adjustResize" to the activity:

enter image description here

The red dashed line is above the keyboard now. My questions are:

  1. Why do I observe such a behavior?
  2. What's wrong with NestedScrollView?
like image 209
Maksim Dmitriev Avatar asked Apr 17 '17 16:04

Maksim Dmitriev


2 Answers

The Android docs say:

When the input method appears on the screen, it reduces the amount of space available for your app's UI. The system makes a decision as to how it should adjust the visible portion of your UI, but it might not get it right. To ensure the best behavior for your app, you should specify how you'd like the system to display your UI in the remaining space.

This could be an instance of the instability that is referenced.

It looks to me that this isn't an issue with NestedScrollView and ScrollView but is more of an issue with how the Activity is adjusting the UI based on the keyboard. The only difference between NestedScrollView and ScrollView is that NestedScrollView has be ability to be both parent and child. I think that this highlights why you should follow the above advice where it says:

To ensure the best behavior for your app, you should specify how you'd like the system to display your UI in the remaining space.

ie using android:windowSoftInputMode="adjustResize" always, rather than just when it is needed.

like image 145
BlackHatSamurai Avatar answered Nov 01 '22 14:11

BlackHatSamurai


The problem here is not Nested Scroll-view, the problem is Android's Auto adjustment of view when keyboard appears/disappears on Input method's call. I hope you'll understand the problem in the link given below:

https://developer.android.com/training/keyboard-input/visibility.html

like image 34
Zohaib Hassan Avatar answered Nov 01 '22 14:11

Zohaib Hassan