6

Let's say I have this simple layout:

https://i.stack.imgur.com/QM9i7.jpg

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

    <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingBottom="80dp"
            android:clipToPadding="false">

        <LinearLayout
                android:id="@+id/container"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:padding="16dp">

            <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@mipmap/ic_launcher" />

            ...


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

        </LinearLayout>

    </ScrollView>

    <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_margin="16dp"
            android:text="Button" />

</RelativeLayout>

Note there is paddingBottom on ScrollView with clipToPadding=false. It's needed so Button looks like floating button over scroll view content. ScrollView padding is used to make space below content to make last child available.

If the last child is EditText I expect ScrollView to scroll making EditText visible over software keyboard. But it ends with this https://i.stack.imgur.com/3hwNy.jpg

Kind of expected behavior can be achieved using layout_marginBottom instead of paddingBottom, but in this case obviously I can't see my content behind Button. Screenshot https://i.stack.imgur.com/KPPwy.jpg

Is there a way to make ScrollView to respect its paddings in terms of keyboard avoiding?

UPDATE: full xml code here https://pastebin.com/P8n0aZ2i

pavelkorolevxyz
  • 1,380
  • 2
  • 14
  • 35
  • Try adding the padding to the last edit text instead of the scroll view – MichaelStoddart Nov 01 '19 at 10:56
  • Give padding to top level view and provide paddingBottom to the linear layout. It will work –  Nov 01 '19 at 11:37
  • If I use padding for `EditText` it's just getting bigger resizing its background (with default material theme underline will be lower) but it doesn't help. – pavelkorolevxyz Nov 01 '19 at 11:39
  • use coordinator layout. – Haider Saleem Nov 08 '19 at 12:07
  • Quick question , can you scroll up after opening soft keyboard ? Because your same xml is working for me using a `ConstraintLayout` on top. After opening soft keyboard I can scroll up revealing the last `EditText` on top of button – Syed Ahmed Jamil Nov 14 '19 at 21:37
  • Also what is wrong with your second last screenshot isn't that what you want ? You last screenshot link is broken. Check that again. – Syed Ahmed Jamil Nov 14 '19 at 21:46
  • @SyedAhmedJamil I should see text behind button. Scroll content is clipped to margin instead. I can scroll up, yes. – pavelkorolevxyz Nov 15 '19 at 08:59
  • Is there a `TextView` below the last `EditText` in your project because you didn't mention text in your xml posted here ? – Syed Ahmed Jamil Nov 15 '19 at 13:25
  • can you show your proper complete xml as it is in your project currently ? – Syed Ahmed Jamil Nov 15 '19 at 13:37
  • @SyedAhmedJamil I added TextView for a better understanding before making screenshots but forgot to update XML. Added link to question. – pavelkorolevxyz Nov 15 '19 at 14:06
  • @P_King Have you tried using `ConstraintLayout` instead of `RelativeLayout`. Because I just literally copy pasted your exact code just excluding the the relative layout tag because I have constraint layout on top by default and it works perfectly. I can scroll to the bottom and I can see the complete text above the middle button. I will post proper answer in a moment with a GIF. – Syed Ahmed Jamil Nov 15 '19 at 14:26
  • @SyedAhmedJamil recorded video. Changed RL for CL on this video. https://imgur.com/a/w1fYQEI – pavelkorolevxyz Nov 15 '19 at 14:50
  • @P_King What is your device on which you are running this on ? I think it is a device specific problem because I just ran it on Pixel 2 XL virtual machine and it works as expected. This time I'm just using your exact code with relative layout. It doesn't looks like a problem with your code but the device maybe. I'll try to emulate your device. Here is the video sample upon running on Pixel 2 XL virtual machine : https://www.dropbox.com/s/67ga970ky5khn6a/ScrollView.mkv?dl=0 – Syed Ahmed Jamil Nov 15 '19 at 15:28
  • @P_King Also can you upload your project without making any changes to dropbox or github etc and send me the link ? Maybe the issue is somewhere else. I'll look into that. – Syed Ahmed Jamil Nov 15 '19 at 15:38
  • @P_King Meanwhile, try adding `android:fitsSystemWindows="true"` attribute in your `RelativeLayout` tag. This has worked for most people with scrolling issue when keyboard is open. You can read further about this here https://medium.com/androiddevelopers/why-would-i-want-to-fitssystemwindows-4e26d9ce1eec#.5x2hz7q0g – Syed Ahmed Jamil Nov 15 '19 at 15:47
  • @SyedAhmedJamil I think you didn't get my initial issue. Scrolling works for me too, it's fine, but if you tap on Eighth `EditText` it will be behind the button. – pavelkorolevxyz Nov 15 '19 at 15:58
  • @P_King I see so you want the eight `EditText` to be initially above the button when you click it . So you don't have to scroll to make it visible but you can still scroll to make it visible right anyway ? Am I correct ? – Syed Ahmed Jamil Nov 15 '19 at 16:10
  • lets [continue in chat](https://chat.stackoverflow.com/rooms/202422/discussion-between-syed-ahmed-jamil-and-p-king) if you like but long story short if you can make visible any content of scroll view by just scrolling down and if you are at the bottom none of the content is overlapped with the middle button just like you see in the video I shared, then I think it is the normal desirable behaviour – Syed Ahmed Jamil Nov 15 '19 at 16:37

4 Answers4

5

you just need to define soft-Input adjustment type in manifest for your activity . You can add following line in your manifest.

 <activity android:name=".Main2Activity" android:theme="@style/BaseTheme_FullScreen2"
              android:windowSoftInputMode="adjustResize|adjustPan">
    </activity>
  • No, it doesn't help. `adjustPan` leaves button behind keyboard and content is not scrolling above keyboard. `adjustResize` stays the same - https://imgur.com/GwOtLIq – pavelkorolevxyz Nov 11 '19 at 07:36
0

If you want to achieve scrolling when keyboard is open you have to adjust the widgets in order that they do not overcome each other.

Add android:layout_above="@+id/button" in the scrollview widget , that way it will adjust itself over button when keyboard is showing.

Code

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

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="80dp"
        android:layout_above="@+id/button"
        android:clipToPadding="false">

        <LinearLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="16dp">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/ic_launcher" />

            ...


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

        </LinearLayout>

    </ScrollView>

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_margin="16dp"
        android:text="Button" />

</RelativeLayout>

enter image description here

Quick learner
  • 10,632
  • 4
  • 45
  • 55
  • That negates the whole point why I'm doing this. I want to see the content behind `Button`. But I also want to have EditText visible on keyboard show. – pavelkorolevxyz Nov 01 '19 at 11:26
0

Use CoordinatorLayout instead of RelativeLayout as below:

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

<ScrollView
        android:id="@+id/scroll_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <LinearLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="16dp">

        <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/ic_launcher" />

        ...


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

    </LinearLayout>

</ScrollView>

<Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        app:layout_anchorGravity=”bottom|end”
        android:layout_margin="16dp"
        app:layout_anchor=”@id/scroll_container” 
        app:layout_dodgeInsetEdges="bottom"
        android:text="Button" />

</CoordinatorLayout>

I have not tested this, but along with setting android:windowSoftInputMode="adjustResize|adjustPan" for activity tag in manifest, it should work as you want.

JavaGhost
  • 406
  • 4
  • 8
0

I don't know very much about android, but as Java Programmer I would like to suggest you to change your scrollView size in Activity class. Please put Your scrollView into some other CordinatorLayout and put below code in init() or onCreate() method

 protected void onCreate(Bundle savedInstanceState) {
   
    mySwipeRefreshLayout.setOnRefreshListener(
            new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                  scrollView.setMinHeight(layoutComposite.computeSize(layoutComposite.getSize().x+100 , SWT.DEFAULT).y+100);
    
                }
            }
    );
}

Call above code in activity refresh/onload/onCreate

Intent intent=new Intent(Current_Activity.this,Current_Activity.class);
    startActivity(intent);
    finish();

refer this link Scroll Implementation

Dheeraj Upadhyay
  • 336
  • 2
  • 12