Question

I have been trying to set the initial position of a ScrollView but have not found a way. Does anyone have any ideas? Also, I have a GoogleMaps fragment as children of the ScrollView.

Was it helpful?

Solution

Yes, that is possible:

ScrollView.scrollTo(int x, int y);
ScrollView.smoothScrollTo(int x, int y);
ScrollView.smoothScrollBy(int x, int y);

can be used for that. The x and y parameters are the coordinates to scroll to on the horizontal and vertical axis.

Example in code:

    @Override   
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_layout);

        ScrollView sv = (ScrollView) findViewById(R.id.scrollView);
        sv.scrollTo(0, 100);
     }

In that example, once the Activity is started, your ScrollView will be scrolled down 100 pixels.

You can also try to delay the scrolling process:

final ScrollView sv = (ScrollView) findViewById(R.id.scrollView);

Handler h = new Handler();

h.postDelayed(new Runnable() {

    @Override
    public void run() {
        sv.scrollTo(0, 100);            
    }
}, 250); // 250 ms delay

OTHER TIPS

The accepted answer is not working for me. There is no direct way to set the initial position of a scroll view.

However, you can set the initial position before drawing the scroll view, like this:

rootView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
    @Override
    public boolean onPreDraw() {
        scrollView.getViewTreeObserver().removeOnPreDrawListener(this);
        scrollView.setScrollY(100);
        return false;
    }
});

You can also use scrollView.setScrollY(100) inside Handler, but that will be jerky while scrolling.

Scrollview sw = (ScrollView) findViewById(R.id.scrollView);
sw.post(new Runnable() {
     public void run() {
           sw.smoothScrollTo(0, 5000);
     }
});

None of the answers worked for me. My ScrollView is part of a surrounding LinearLayout. I put the adjustment of the ScrollView into the onDraw() method of this LinearLayout like this:

@Override
protected void onDraw(Canvas canvas){
    if(scrollAdjustTries > 2){
        return;
    }
    scrollAdjustTries++;
    mScrollView.scrollTo(0, mModel.scrollLine());
}

Very ugly, but working.

After several hours internet searching

this worked for me :

ScrollView.post(new Runnable() {
@Override
public void run() {
//setting position here :
        ScrollView.scrollTo(X, Y);
    } 
});

The problem is that the ScrollView doesn't know its size before it been laid out. A solution is to save the scroll position unil onLayout has been called and then set the scroll position.

Here is an example (kotlin):

class MyScrollView constructor(context: Context, attrs: AttributeSet) : ScrollView(context, attrs) {

    private var hasStartPositionBeenSet = false
    var startPosition = 0

    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
        super.onLayout(changed, l, t, r, b)

        if(!hasStartPositionBeenSet) {
             scrollTo(0, startPosition)
             hasStartPositionBeenSet = true
        }
    }
}

class MyActivity: Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        /* ... */
        findViewById<MyScrollView>(R.id.scrollView).startPosition = scrollPosition
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top