How to disable and enable the scrolling on android ScrollView?

Prosanto picture Prosanto · Sep 19, 2013 · Viewed 75.9k times · Source

I am a android developer.I also want to use a ScrollView.This ScrollView need to some time disable scrolling and Some time enable scrolling .But i can no able to disable the scrolling .How to i implement it .Please help to me.I also try to use the some code such a s

fullparentscrolling.setHorizontalFadingEdgeEnabled(false);
fullparentscrolling.setVerticalFadingEdgeEnabled(false);

or

 fullparentscrolling.setEnabled(false);

But it does not work.

Answer

Biraj Zalavadia picture Biraj Zalavadia · Sep 19, 2013

Try this way

Create Your CustomScrollview like this

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ScrollView;

public class CustomScrollView extends ScrollView {

    private boolean enableScrolling = true;

    public boolean isEnableScrolling() {
        return enableScrolling;
    }

    public void setEnableScrolling(boolean enableScrolling) {
        this.enableScrolling = enableScrolling;
    }

    public CustomScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public CustomScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomScrollView(Context context) {
        super(context);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {

        if (isEnableScrolling()) {
            return super.onInterceptTouchEvent(ev);
        } else {
            return false;
        }
    }
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
       if (isEnableScrolling()) {
            return super.onTouchEvent(ev);
       } else {
           return false;
       }
}
}

In your xml

// "com.example.demo" replace with your packagename

<com.example.demo.CustomScrollView
        android:id="@+id/myScroll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </com.example.demo.CustomScrollView>

In your Activity

CustomScrollView myScrollView = (CustomScrollView) findViewById(R.id.myScroll);
        myScrollView.setEnableScrolling(false); // disable scrolling
        myScrollView.setEnableScrolling(true); // enable scrolling