Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scroll to center of HorizontalScrollView only when activity starts?

I have a activity that shows a howizontalscrollview with a horizontally long imageview.

I need that when the activity starts, the horizontalscrollview must be scrolled to the center of itselft, and not to the start.

I'm searching on google and here and i can't find the way..

this is my horizontal scroll view:

    HorizontalScrollView wvScroll = new HorizontalScrollView(this);
    wvScroll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
    iv.setImageBitmap(Util.getRemoteImage("http://mywebsite.com/90.gif"));
    iv.setScaleType(ScaleType.CENTER_CROP);
    wvScroll.addView(iv);
    mainLayout.addView(wvScroll);

Thanks

like image 404
Pableras84 Avatar asked Oct 17 '12 18:10

Pableras84


People also ask

Can scroll horizontally Android?

HorizontalScrollView is used to scroll the child elements or views in a horizontal direction. HorizontalScrollView only supports horizontal scrolling. For vertical scroll, android uses ScrollView.


1 Answers

Try:

wvScroll.scrollTo(offsetX, 0);

There may be a timing issue, where the offset will be set before the scrollView is created. In that case, use postDelayed() with a runnable that calls scrollTo

ADDED:

To use postDelayed():

private Handler mHandler = new Handler();

in onCreate do:

mHandler.postDelayed(new Runnable(){
    public void run() {
       wvScroll.scrollTo(offsetX, 0);
    }
}, 1000);
like image 132
Tamir Scherzer Avatar answered Oct 19 '22 09:10

Tamir Scherzer