Search code examples
androidsmooth-scrolling

Smooth scrolling in scrollView


I am new to android and I have created a simple block of code which has a scrollView and an image. I want this scrollview to scroll smoothly. What do I have to do for that ? I have gone through many materials over internet, but all of those use hard terminologies which I'm not able to understand. Can someone please help me ? Thanks.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <ScrollView
            android:id="@+id/scroll_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/colorPrimaryDark">
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/text_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                android:text="@string/top_string"
                android:textColor="@android:color/white"
                android:textSize="16dp"
                android:textScaleX="1.5" 
            />
             <ImageView
                 android:id="@+id/img"
                 android:layout_width="300dp"
                 android:layout_height="150dp"
                 android:src="@drawable/dg_truck_main"
                 android:layout_gravity="center_horizontal"
                 android:background="@android:color/holo_blue_light"
                 android:layout_marginTop="10dp"
                 android:layout_centerHorizontal="true"
                 android:layout_below="@id/text_view"
              />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/top_string2"
            android:textColor="@android:color/white"
            android:textSize="16dp"
            android:textScaleX="1.5"
            android:layout_marginTop="50dp"
            android:layout_below="@id/img"
            />
            </RelativeLayout>
    </ScrollView>

</RelativeLayout>

Solution

  • You have added ScrollView inside RelativeLayout, what I suggest is that you should place RelativeLayout inside ScrollView tag. Please go through below code,

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView
            android:id="@+id/scroll_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/colorPrimaryDark">
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        xmlns:android="http://schemas.android.com/apk/res/android">
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/text_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                android:text="@string/top_string"
                android:textColor="@android:color/white"
                android:textSize="16dp"
                android:textScaleX="1.5" 
            />
             <ImageView
                 android:id="@+id/img"
                 android:layout_width="300dp"
                 android:layout_height="150dp"
                 android:src="@drawable/dg_truck_main"
                 android:layout_gravity="center_horizontal"
                 android:background="@android:color/holo_blue_light"
                 android:layout_marginTop="10dp"
                 android:layout_centerHorizontal="true"
                 android:layout_below="@id/text_view"
              />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/top_string2"
            android:textColor="@android:color/white"
            android:textSize="16dp"
            android:textScaleX="1.5"
            android:layout_marginTop="50dp"
            android:layout_below="@id/img"
            />
            </RelativeLayout>
    
    </RelativeLayout>
    </ScrollView>