Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7936659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:16:56+00:00 2026-06-03T22:16:56+00:00

I am trying to set the position of the horizontal scrollview so it corresponds

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T22:16:58+00:00Added an answer on June 3, 2026 at 10:16 pm

    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():

    @Override
    public 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.MyHorizontalScrollView
    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" />
    

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

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
    xmlns: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>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to absolutely position a TinyMCE editor at a set position using
I'm trying to place a button. I have its position set to absolute, so
I've got this piece of code where I'm trying to position a set of
I'm trying to absolutely position some floated divs using this code: $(function() { $('#wrapper
I'm trying to set position to view at activity start. I found out that
I'm trying to set the position of the Messages.app chat window using AppleScript. tell
I'm trying to set the caret position in a contenteditable div layer, and after
I am trying to set my gridview (asp.net) position to absolute when the gridview
I am trying to set the Media Elements position by following code: MediaElement musicPlayer
I am having a real headache trying to set a node's local position to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.