I have a horizontal scroll bar with icons (buttons) on it.
I try to move the selected icon to the middle of the bar.
I have read this post about positioning views in android.
I have got this code, which seems logically ok to me:
public void selectButton() {
...
HorizontalScrollView sv=(HorizontalScrollView)button.getParent().getParent();
int offsetX=getButtonXPosition()-sv.getWidth()/2;
sv.smoothScrollTo(offsetX, 0);
..
}
public int getButtonXPosition() {
return (button.getLeft()+button.getRight())/2;
}
I move left top corner a
pixels to right\left (negative\positive number of pixels),
where a
= X position of middle of selected button
-
middle of the bar
.
meaning I want to move the middle of the button a
pixels to the right\left (negative\positive)
However the buttons stop too left\right (if it's the rightmost\leftmost button)
see images attached: