Search code examples
androidviewfocusandroid-linearlayouthorizontalscrollview

HorizontalScrollView - auto scroll to a selected view


I have a HorizontalScrollView, inside the horizontal I have a linearlayout (orientation=horizontal) with 10 views.

I'm trying to show the horizontalscrollview auto scrolled to a selected view.

I've done this requesting focus from the selected view:

li.setFocusable(true);

The problem is that the view apperas at the right of the screen and I'm trying to show the view centered.

Any idea?

Thanks


Solution

  • You can use smoothScrollTo(int x, int y) method, (x, y) represents the position of selected view, you can calculate it easily: (View.getLeft() + View.getPaddingLeft(), View.getTop()).