I am trying to set the position of the horizontal scrollview so it corresponds with the button that is pushed. I’ve tried using this to set it unsuccessfully:
HorizontalScrollView hsv = (HorizontalScrollView)findViewById(R.id.ScrollView);
int x, y;
x = hsv.getLeft();
y = hsv.getTop();
hsv.scrollTo(x, y);
This results in nothing, the scrollview is unaffected.
The xml:
<HorizontalScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ScrollView"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="@null"
android:scrollbars="none" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<Button
android:layout_width="100dp"
android:layout_height="fill_parent"
android:layout_marginBottom="-5dp"
android:text="btn0"
android:id="@+id/btn0"
android:background="@drawable/yellow_btn" />
<Button
android:layout_width="100dp"
android:layout_height="fill_parent"
android:layout_marginBottom="-5dp"
android:background="@drawable/yellow_btn"
android:text="bnt1"
android:id="@+id/btn1" />
<Button
android:layout_width="100dp"
android:layout_height="fill_parent"
android:layout_marginBottom="-5dp"
android:background="@drawable/yellow_btn"
android:text="btn2"
android:id="@+id/btn2" />
<Button
android:layout_width="100dp"
android:layout_height="fill_parent"
android:layout_marginBottom="-5dp"
android:background="@drawable/yellow_btn"
android:text="btn3"
android:id="@+id/btn3" />
<Button
android:layout_width="100dp"
android:layout_height="fill_parent"
android:layout_marginBottom="-5dp"
android:background="@drawable/yellow_btn"
android:text="btn4"
android:id="@+id/btn4" />
<Button
android:layout_width="100dp"
android:layout_height="fill_parent"
android:layout_marginBottom="-5dp"
android:background="@drawable/yellow_btn"
android:text="btn5"
android:id="@+id/btn5" />
</LinearLayout>
</HorizontalScrollView>
So if the 5th button is pushed (which is offscreen) when the new activity that starts I want to set the new view so the horizontal scrollview is all the way to the right versus starting out all the way to the left.
How can I set the position of the horizontal scrollview?
Right now you are trying to scroll to the top-left corner of the HorizontalScrollView rather than the position of the button. Try scrolling to the (x, y) position of the button like this:
EDIT:
This code will not behave as you would expect if it is placed in
onCreate(). Even though you have calledsetContentView(), the layout has not been measured and initialized yet. This means that thegetLeft()andgetTop()methods will both return 0. Trying to set the scroll position before the layout is fully initialized has no effect, so you need to callhsv.scrollTo()some time afteronCreate().One option that seems to work is placing the code in
onWindowFocusChanged():However, this function is called every time the Activity gains or loses focus, so you might end up updating the scroll position more often than you intended.
A more elegant solution would be to subclass the HorizontalScrollView and set the scroll position in
onMeasure(), after you know that the view has been initialized. To do this, I split your layout into two files and added a new class named MyHorizontalScrollView:When MyHorizontalScrollView is created, it automatically inflates and adds the button layout. Then after calling the super
onMeasure()(so that it knows the layout has finished initializing) it sets the scroll position.This is the new main.xml. It only contains the new MyHorizontalScrollView, though you could easily put it inside of a Linear or Relative layout and add other view elements. (You would replace
com.theisenp.testwith the name of the package where MyHorizontalScrollView is located):And this is the buttons.xml layout that is automatically inflated by MyHorizontalScrollView: