Android setting position of hortizontal scrollview Android setting position of hortizontal scrollview android android

Android setting position of hortizontal 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:

HorizontalScrollView hsv = (HorizontalScrollView) findViewById(R.id.ScrollView);Button button = (Button) findViewById(R.id.btn5);int x, y;x = button.getLeft();y = button.getTop();hsv.scrollTo(x, y);

EDIT:

This code will not behave as you would expect if it is placed in onCreate(). Even though you have called setContentView(), the layout has not been measured and initialized yet. This means that the getLeft() and getTop() methods will both return 0. Trying to set the scroll position before the layout is fully initialized has no effect, so you need to call hsv.scrollTo() some time after onCreate().

One option that seems to work is placing the code in onWindowFocusChanged():

@Overridepublic void onWindowFocusChanged(boolean hasFocus) {    super.onWindowFocusChanged(hasFocus);    HorizontalScrollView hsv = (HorizontalScrollView) findViewById(R.id.ScrollView);    Button button = (Button) findViewById(R.id.btn5);    int x, y;    x = button.getLeft();    y = button.getTop();    hsv.scrollTo(x, y);}

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:

package com.theisenp.test;import android.content.Context;import android.util.AttributeSet;import android.view.View;import android.widget.HorizontalScrollView;public class MyHorizontalScrollView extends HorizontalScrollView {    public MyHorizontalScrollView(Context context) {        super(context);        addButtons(context);    }    public MyHorizontalScrollView(Context context, AttributeSet attrs) {        super(context, attrs);        addButtons(context);    }    /**     * Inflates the layout containing the buttons and adds them to the ScrollView     * @param context     */    private void addButtons(Context context) {        View buttons = inflate(context, R.layout.buttons, null);        addView(buttons);    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        super.onMeasure(widthMeasureSpec, heightMeasureSpec);        //Find button 5 and scroll to its location        View button = findViewById(R.id.btn5);        scrollTo(button.getLeft(), button.getTop());    }}

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.test with the name of the package where MyHorizontalScrollView is located):

<?xml version="1.0" encoding="utf-8"?><com.theisenp.test.MyHorizontalScrollViewxmlns: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" />

And this is the buttons.xml layout that is automatically inflated by MyHorizontalScrollView:

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="wrap_content"android:layout_height="fill_parent"android:orientation="horizontal" >    <Button    android:id="@+id/btn0"    android:layout_width="100dp"    android:layout_height="fill_parent"    android:layout_marginBottom="-5dp"    android:text="btn0" />    <Button    android:id="@+id/btn1"    android:layout_width="100dp"    android:layout_height="fill_parent"    android:layout_marginBottom="-5dp"    android:text="bnt1" />    <Button    android:id="@+id/btn2"    android:layout_width="100dp"    android:layout_height="fill_parent"    android:layout_marginBottom="-5dp"    android:text="btn2" />    <Button    android:id="@+id/btn3"    android:layout_width="100dp"    android:layout_height="fill_parent"    android:layout_marginBottom="-5dp"    android:text="btn3" />    <Button    android:id="@+id/btn4"    android:layout_width="100dp"    android:layout_height="fill_parent"    android:layout_marginBottom="-5dp"    android:text="btn4" />    <Button    android:id="@+id/btn5"    android:layout_width="100dp"    android:layout_height="fill_parent"    android:layout_marginBottom="-5dp"    android:text="btn5" /></LinearLayout>


Although, this is an old question, I recently run into the same problem and found yet another but rather less complex solution.

Let's assume the given layout file in the original question and let's assume that if an activity is started with the given layout, the horizontal scrollview needs to be scrolled to button 5.

To achieve scrolling to button 5, put the following code in the onCreate() method of the activity:

@Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        // Some code such as layout inflation.        final HorizontalScrollView hsv = (HorizontalScrollView) findViewById(R.id.ScrollView);        // Scrolling to button 5.        hsv.post(new Runnable() {            @Override            public void run() {                // Get the button.                View button = findViewById(R.id.btn5);                // Locate the button.                int x, y;                x = button.getLeft();                y = button.getTop();                // Scroll to the button.                hsv.scrollTo(x, y);            }        });    }}