Looks like I can't get or set any position or scrolling amount in my simple layout:
// Relative layout - main_layout
<HorizontalScrollView
android:id="@+id/MenuScroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="48dp"
android:orientation="horizontal"
android:layout_gravity="center"
android:padding="0dp" >
<Button
android:id="@+id/button1"
android:layout_width="150dp"
android:layout_height="fill_parent" />
<Button
android:id="@+id/button2"
android:layout_width="150dp"
android:layout_height="fill_parent" />
<Button
android:id="@+id/button3"
android:layout_width="150dp"
android:layout_height="fill_parent" />
<Button
android:id="@+id/button4"
android:layout_width="150dp"
android:layout_height="fill_parent" />
<Button
android:id="@+id/button5"
android:layout_width="150dp"
android:layout_height="fill_parent" />
</LinearLayout>
</HorizontalScrollView>
// End of RelativeLayout - main_layout
What I want in my MainActivity
is to get the position of any button inside the HorizontalScrollView
and scroll to that position. At the moment I can't get the left value (it's always 0) and if I try to scrollTo
the HorizontalScrollView
it just doesn't, regardless of what the value for x or y is.
// imports
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
final Button button3 = (Button)findViewById(R.id.button3);
Rect rectf = new Rect();
button3.getLocalVisibleRect(rectf);
Log.i("Left: ", String.valueOf(rectf.left)); // it is always 0
HorizontalScrollView MenuScroll = (HorizontalScrollView)findViewById(R.id.MenuScroll);
// Trying to scroll to X=100 but it doesn't
MenuScroll.scrollTo(100, MenuScroll.getBottom());
}
}
So, as I said, I can't get the left position for my button and either can scroll to some X position in my HorizontalScrollView
. Is this possible?