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

I have an activity that shows howizontalscrollview view with a horizontal long image view.

I need that when starting this action, horizontalscrollview should scroll to the center of its area, and not to the beginning.

I search on google and here, and I cannot find a 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

+7
source share
2 answers

Try:

 wvScroll.scrollTo(offsetX, 0); 

There may be a synchronization problem when the offset is set before creating scrollView. In this case, use postDelayed() with runnable , which 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); 
+5
source

I'm not sure, but you probably need to have the height and width of the child. Smth like this:

 int centerX = wvScroll.getChildAt(0).getWidth()/2; int centerY = wvScroll.getChildAt(0).getHeight()/2; wvScroll.scrollTo(centerX, centerY); 
+3
source

All Articles