23

I have a HorizontalScrollView that is filled dynamically with TextViews. Sometime there is one, two, three or more elements. What I'm looking for is a way to have the items centered regardless of the numbers of elements.

For example, With one element:

| --------- MenuA --------- |

With two elements:

| ---- MenuA --- MenuB ---- |

With three elements:

| - MenuA - MenuB - MenuC - |

With five elements (MenuD and MenuE are hidden):

| MenuA - MenuB - MenuC - Me|

Update: Here is a simple layout with two menus. How do I center MenuA and MenuB?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <HorizontalScrollView android:id="@+id/horizontalScrollView1"
        android:layout_width="wrap_content" android:layout_height="wrap_content">
        <LinearLayout android:id="@+id/linearLayout1"
            android:layout_width="fill_parent" android:layout_height="fill_parent"
            android:orientation="horizontal">
            <TextView android:text="MenuA" android:id="@+id/textView1"
                android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
            <TextView android:text="MenuB" android:id="@+id/textView2"
                android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
        </LinearLayout>
    </HorizontalScrollView>
</LinearLayout>
2
  • So your ScollView is horizontal (wider than the screen), the items are screen-centered?
    – Phonon
    Commented Mar 29, 2011 at 20:30
  • Yes, my HorizontalScrollView is horizontal, and the items should be centered horizontally Commented Mar 29, 2011 at 20:36

1 Answer 1

32

use the gravity attribute :

android:layout_gravity="center_horizontal" EDIT : 03.30 :

I found it ! only had to set the gravity to the upper LinearLayout : all you need to do is add a little padding/margins to make the textviews more comfortable to read !

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical" >

    <HorizontalScrollView
        android:id="@+id/horizontalScrollView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal" >

        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/TextView04"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="MenuB" >
            </TextView>

            <TextView
                android:id="@+id/TextView03"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="MenuB" >
            </TextView>

            <TextView
                android:id="@+id/TextView02"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="MenuB" >
            </TextView>

            <TextView
                android:id="@+id/TextView01"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="MenuB" >
            </TextView>

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="MenuA" >
            </TextView>

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="MenuB" >
            </TextView>
        </LinearLayout>
    </HorizontalScrollView>

</LinearLayout>

keep me posted !

4
  • 2
    I tried settings android:layout_gravity="center_horizontal" and the TextViews are still located on the right Commented Mar 29, 2011 at 20:38
  • 1
    could you please send your full layout ?
    – olamotte
    Commented Mar 29, 2011 at 20:42
  • 1
    It should actually just be android:gravity="center_horizontal", , since layout_gravity will just place the HorizontalScrollView at the parent's horizontal center, but offhand, I'm not quite sure even that will work as you're expecting.. Commented Mar 29, 2011 at 21:33
  • 1
    Worked for me after a small tweak. My layout contained a LinearLayout inside a HorizontalScrollView, I had to wrap the two of them in a new LinearLayout purely to assign the orientation and gravity to this.
    – r1chjc
    Commented Jun 3, 2016 at 14:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.