I’m working on a Family Tree app. Now, the tree view can be large, and I allow the users to navigate left and right (for example if you have 7 siblings). But when I open the tree view, it always start from the left corner and you can scroll to the right to see more. I want to tree view to open always in the middle of the layout.
Here is the code:
<?xml version="1.0" encoding="UTF-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/HorizontalScrollView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/light_green" >
<LinearLayout
android:id="@+id/Top_Level_View_Family_Tree"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/TextView_view_family_members_title"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:text="Family Tree"
android:gravity="center_horizontal"
android:textSize="@dimen/font_large" />
<LinearLayout
android:id="@+id/Family_Tree_parentLL"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:gravity="center_horizontal"
android:orientation="horizontal" >
</LinearLayout>
<LinearLayout
android:id="@+id/Family_Tree_yourLL"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:gravity="center_horizontal"
android:orientation="horizontal" >
</LinearLayout>
<LinearLayout
android:id="@+id/Family_Tree_siblingLL"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:gravity="center_horizontal"
android:orientation="horizontal" >
</LinearLayout>
<LinearLayout
android:id="@+id/Family_Tree_childLL"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:gravity="center_horizontal"
android:orientation="horizontal" >
</LinearLayout>
</LinearLayout>
</HorizontalScrollView>
Here is the app:
http://img838.imageshack.us/img838/310/appa.png
Great Help:
Android calculate scrollTo position in HorizontalScrollView
You’ll have to position it in code, can’t do it in XML. Use the
scrollTomethod ofHorizontalScrollViewto accomplish this. You’ll have to calculate the position that you need to scroll to yourself based on the width of the top level LinearLayout.Something like the following:
Now, you’re going to need to edit that
scrollTopart because I’m sure it’s not the right position to scroll to, it probably scrolls to a position on the left side of the view, and what I did assumes that it goes to the center. You’ll have to play around with the calculation to make it do what you want.