Jane
Jane

Reputation: 43

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>

Upvotes: 1

Views: 9964

Answers (4)

Priyavrat Talyan
Priyavrat Talyan

Reputation: 121

I had a hard time resolving this issue. Finally found a solution very simple and very smooth outcome. In your recycler view set nested scrolling to false. Two ways to achieve it.

  1. Through xml file -

    android:nestedScrollingEnabled="false"

  2. Through java code -

    recyclerView.isNestedScrollingEnabled = false

Upvotes: 1

Mazhar Iqbal
Mazhar Iqbal

Reputation: 999

//to make the scrollview faster //add the below line in xml

  android:fillViewport="true"

//also add the following line to your java class // to make the scroll view faster

    scrollView.fullScroll(View.FOCUS_DOWN);
    scrollView.setSmoothScrollingEnabled(true);

Upvotes: 0

Jane
Jane

Reputation: 43

Okay I got my answer. Since I was new to android, I thought that "smooth scroll" is something else and we have to integrate it into our project. But a scrollView already gives smooth scrolling and so we do not need to write any extra code for that.

Upvotes: 0

Pranay Soni
Pranay Soni

Reputation: 403

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>

Upvotes: 2

Related Questions