Ahmed Emad Ahmed Emad - 4 months ago 11
Android Question

Android HorizontalScrollView content Stretch

I have a HorizontalScrollView with some buttons, I am trying to stretch these buttons to fill the whole width of the HorizontalScrollView so that they can appear organized and equally spaced between each others. Here is the Image which I have.
HorizontalScrollView content Unstretched

I want these 4 buttons to be stretched over the whole width! here is my code

<HorizontalScrollView
android:id="@+id/items_HorizontalBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/vf"
android:background="#80000000"
android:fillViewport="true" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >

<Button
android:layout_width="90dp"
android:layout_height="fill_parent"
android:layout_margin="5dp"
android:background="@android:color/transparent"
android:drawableTop="@drawable/rss"
android:text="Light"
android:textColor="@color/green_color"
android:textSize="20sp" />

<Button
android:layout_width="90dp"
android:layout_height="fill_parent"
android:layout_margin="5dp"
android:background="@android:color/transparent"
android:drawableTop="@drawable/rss"
android:text="Door"
android:textColor="@color/green_color"
android:textSize="20sp" />

<Button
android:layout_width="90dp"
android:layout_height="fill_parent"
android:layout_margin="5dp"
android:background="@android:color/transparent"
android:drawableTop="@drawable/rss"
android:text="Alarms"
android:textColor="@color/green_color"
android:textSize="20sp" />

<Button
android:layout_width="90dp"
android:layout_height="fill_parent"
android:layout_margin="5dp"
android:background="@android:color/transparent"
android:drawableTop="@drawable/rss"
android:text="Window"
android:textColor="@color/green_color"
android:textSize="20sp" />
</LinearLayout>
</HorizontalScrollView>

Answer

For equal distributions, set the weightSum value of the parent, and assign layout_weight to its children.

For 4 equal sized buttons, add android:weightSum="1" to the LinearLayout. For each of the buttons set android:layout_width="0dp", then add android:layout_weight="0.25".

This is what occurs in the code but depending on the View, the "Distribute Weights Evenly" button in the Eclipse GUI can also help.

However, HorizontalScrollView can only host one direct child, I wonder about the structure of this layout...