8

I have one background image which have RelativeLayout as it's parent and sibling as ScrollView and ScrollView have one EditText. I am facing problem where background image moving top side when we are clicking Edittext. Please find below code snippet and attached screenshots. How we can restrict scrollview from doing this?

Code:

<ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:src="@drawable/dog"
        android:scaleType="centerCrop"/>

<ScrollView
        android:id="@+id/layout_scroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:visibility="visible">

    <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">


        <EditText
                android:id="@+id/postal_code"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Postcode"
        />


    </RelativeLayout>
</ScrollView>

Before:

enter image description here

After:

enter image description here

Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147

6 Answers6

5

Add this in Activity Manifest

android:windowSoftInputMode="adjustNothing"
Chandan kushwaha
  • 941
  • 6
  • 26
4

Change ScrollView to androidx.core.widget.NestedScrollView if you use androidx or NestedScrollView from support package. If you didn't hide anything in the top of your xml this is gonna work(just checked it in test project).

0

When soft keyboard opens, Activity height changes and the ImageView crop the image again. This is the problem.
Here is the idea to solve this issue: if ImageView crops the source image just from the bottom instead of both top and bottom, then the top of image will be fixed always and you won't see this problem anymore.
So, you need a custom ImageView that crops images only from the bottom. Thanks to @Dori and his great answer to provide ImageViewTopCrop.

/**
 * @author doriancussen
 */
public class ImageViewTopCrop extends ImageView
{
    public ImageViewTopCrop(Context context)
    {
        super(context);
        setScaleType(ScaleType.MATRIX);
    }

    public ImageViewTopCrop(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        setScaleType(ScaleType.MATRIX);
    }

    public ImageViewTopCrop(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        setScaleType(ScaleType.MATRIX);
    }

    @Override
    protected boolean setFrame(int l, int t, int r, int b)
    {
        Matrix matrix = getImageMatrix();
        float scaleFactor = getWidth()/(float)getDrawable().getIntrinsicWidth();
        matrix.setScale(scaleFactor, scaleFactor, 0, 0);
        setImageMatrix(matrix);
        return super.setFrame(l, t, r, b);
    }
}

Modify your xml layout like below:

<?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">

    <com.doridori.lib.view.ImageViewTopCrop
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/dog" />

    <ScrollView
        android:id="@+id/layout_scroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:visibility="visible">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <EditText
                android:id="@+id/postal_code"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Postcode" />

        </RelativeLayout>
    </ScrollView>
</RelativeLayout>

Important: Be careful about dog image aspect ratio. Its height should be long enough to fill screen after scalling the image based on width of screen.

PS: If this answer is helpful don't forget to upvote @dori answer.

Mir Milad Hosseiny
  • 2,769
  • 15
  • 19
0
<android.support.constraint.ConstraintLayout 
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="com.unicode.attax.Activity.LoginActivity">

    <ImageView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:src="@drawable/background_login_3x"
        android:scaleType="centerCrop"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </ImageView>
</android.support.constraint.ConstraintLayout>

I use this code to use background image in Scrollview. You can manage the ratio and scaletype.

Rembulan Moon
  • 338
  • 3
  • 11
0

Add inside Activity tag in your manifest file

android:windowSoftInputMode="stateVisible|adjustPan"
Siddarth Jain
  • 304
  • 1
  • 6
0

So, your problem is actually caused by layout resizing when keyboard shows up.

To avoid this you can add following line to your manifest (in activity tag)

android:windowSoftInputMode="adjustNothing"

Something like below

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.....">
<uses-permission ....
....
    <application
        android:allowBackup="true"
        ...>

         <activity android:name=".YourActivity" 
             ...
             android:windowSoftInputMode="adjustNothing">
             ....
         </activity>
    </application>

You may also refer to this question and this answer

Mayank Kumar Chaudhari
  • 16,027
  • 10
  • 55
  • 122