15

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?
Maksim Dmitriev
  • 5,313
  • 9
  • 57
  • 124

3 Answers3

4

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.

BlackHatSamurai
  • 21,845
  • 20
  • 84
  • 147
  • Thx. Updated @davidgro – BlackHatSamurai Apr 25 '17 at 22:41
  • `but is more of an issue with how the Activity is adjusting the UI based on the keyboard` Yes, but it's adjusting one way when it is `ScrollView` and another way when it is `NestedScrollView`. So the question is, what is the significant difference between these two, that makes this adjustment be different? – azizbekian Apr 26 '17 at 08:03
  • The only difference between the 2 classes is that `NestedScrollView` can be both child AND parent view, where `ScrollView` can only be a parent view. – BlackHatSamurai Apr 26 '17 at 16:05
  • ScrollView is part of platform so Activity class (or whichever component actually handles the resizing) knows about it and can handle it in a special way. NestedScrollView technically has nothing to do with ScrollView, was added later as part of support library, and as far as Activity is concerned, it's just another view. My best guess. – Eugen Pechanec Apr 29 '17 at 21:18
1

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

Zohaib Hassan
  • 944
  • 2
  • 7
  • 11
-3

Why You use fix size.

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

try with

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

before start anything just make rough paper work what kind of design you want.

try this may be work for you.

Vasudev Vyas
  • 630
  • 1
  • 8
  • 24