Android calculate scrollTo position in HorizontalScrollView
<p>I have HorizontalScrollView with few TextViews. Each TextView might contain text of different length.</p>

<p>How should I calculate correct offset to scroll to using "scrollTo(x, y)"? </p>

<p>Scrolling works, but different than expected.
I've tried various things (getLocationOnScreen(), getMeasuredWidth(), getWidth() on each child element and them adding them togetehr) but I still can't get result I like. View is scrolled a bit too far or not enough.</p>

<p><strong>Solution</strong></p>

<p>When HSV has multiple elements with different width you can center on each of them like this:</p>

<pre><code>for (TextView el : requiresFocus) {
    if (el.getText().equals(requiresFocus)) {
        offsetX = ((el.getLeft() + el.getRight()) / 2) - (myHSV.this.getWidth() / 2);
        scrollTo(offsetX, 0);
        break;
    }
}
</code></pre>

<p>with <code>((el.getLeft() + el.getRight()) / 2)</code> you will get center of the element and with <code>(myHSV.this.getWidth() / 2)</code> you will get center of the screen.</p>
