7

In my layout, I have an EditText in a NestedScrollView. When the EditText is edited with a soft keyboard, I would like the NestedScrollView's content to be scrolled up. However, instead, the whole contents of the activity is scrolled away, including an ImageView that shouldn't be moved:

Before After

What I would like (bad Gimp-job):

enter image description here

The below is a simplified version of my layout that still exhibits the problem:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="150dp"
            app:srcCompat="@mipmap/ic_launcher" />

        <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true"
            android:isScrollContainer="true"
            android:paddingTop="150dp">

            <LinearLayout
                android:id="@+id/layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/textView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Aaa aaaaaaaaaa aaa aaaaaaaa aaaa. Aaa aaaaaaa aaa aa aaa aaaaaa aaaaa. Aaaaaaaa aaaaaaaaaaa aa aaaaaaaa aaaa:Aaa aaaaaaaaaa aaa aaaaaaaa aaaa. Aaa aaaaaaa aaa aa aaa aaaaaa aaaaa. Aaaaaaaa aaaaaaaaaaa aa aaaaaaaa aaaa: Aaa aaaaaaaaaa aaa aaaaaaaa aaaa. Aaa aaaaaaa aaa aa aaa aaaaaa aaaaa. Aaaaaaaa aaaaaaaaaaa aa aaaaaaaa aaaa:  Aaa aaaaaaaaaa aaa aaaaaaaa aaaa. Aaa aaaaaaa aaa aa aaa aaaaaa aaaaa. Aaaaaaaa aaaaaaaaaaa aa aaaaaaaa aaaa:  aaa aaaaaa aaaaa. Aaaaaaaa aaaaaaaaaaa aa aaaaaaaa aaaa aa:  aaa aaaaaa aaaaa. Aaaaaaaa aaaaaaaaaaa aa aaaaaaaa  aa:  aaa aaaaaa aaaaa. Aaaaaaaa aaaaaaaaaaa aa aaaaaaaa " />

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:ems="10"
                    android:inputType="text" />

            </LinearLayout>
        </android.support.v4.widget.NestedScrollView>

    </FrameLayout>
</android.support.design.widget.CoordinatorLayout>

I've tried various combinations of windowSoftInputMode on the activity and isScrollContainer on the NestedScrollView, but no combination gives me what I would like: when the soft keyboard is popped up, I would like the text to be scrolled up, without changing the NestedScrollView's visible margin or the ImageView behind it.

Cactus
  • 25,576
  • 9
  • 60
  • 130

2 Answers2

1
<?xml version="1.0" encoding="utf-8"?>
 <android.support.design.widget.CoordinatorLayout 
 xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:fitsSystemWindows="true"
   tools:context=".MainActivity">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        app:srcCompat="@mipmap/ic_launcher" />

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:layout_below="@+id/image"
        android:isScrollContainer="true">

        <LinearLayout
            android:id="@+id/layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/textView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Aaa aaaaaaaaaa aaa aaaaaaaa aaaa. Aaa aaaaaaa aaa aa aaa aaaaaa aaaaa. Aaaaaaaa aaaaaaaaaaa aa aaaaaaaa aaaa:Aaa aaaaaaaaaa aaa aaaaaaaa aaaa. Aaa aaaaaaa aaa aa aaa aaaaaa aaaaa. Aaaaaaaa aaaaaaaaaaa aa aaaaaaaa aaaa: Aaa aaaaaaaaaa aaa aaaaaaaa aaaa. Aaa aaaaaaa aaa aa aaa aaaaaa aaaaa. Aaaaaaaa aaaaaaaaaaa aa aaaaaaaa aaaa:  Aaa aaaaaaaaaa aaa aaaaaaaa aaaa. Aaa aaaaaaa aaa aa aaa aaaaaa aaaaa. Aaaaaaaa aaaaaaaaaaa aa aaaaaaaa aaaa:  aaa aaaaaa aaaaa. Aaaaaaaa aaaaaaaaaaa aa aaaaaaaa aaaa aa:  aaa aaaaaa aaaaa. Aaaaaaaa aaaaaaaaaaa aa aaaaaaaa  aa:  aaa aaaaaa aaaaa. Aaaaaaaa aaaaaaaaaaa aa aaaaaaaa " />

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="text" />

        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>

</RelativeLayout>

TRY THIS CODE IN THIS CODE I HAVE REPLACED FRAME LAYOUT WITH RELATIVE LAYOUT AND SET NESTED SCROLL VIEW BELOW THE IMAGE

Jay Thummar
  • 1,927
  • 1
  • 11
  • 18
  • But in my real app, I need the `NestedScrollView` to cover the whole activity, so that it plays nicely with [toolbar auto-hiding](https://stackoverflow.com/q/50275244/477476); which is why I'm using a padding to reserve space for the `ImageView`. – Cactus May 16 '18 at 13:24
0

Nested Scroll View Displays all the element which are present in it,

if you do not want to increase size of your editText the just keep its fixed height. ex 80dp

 <EditText
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:ems="10"
                    android:inputType="text" />

Hope after keeping fix height to EditText your problem will be solved

Amin Pinjari
  • 1,693
  • 2
  • 19
  • 48