0

I have a LinearLayout that covers the entire width of the screen, inside I have a random number of TextView (7 in this example). I want all the TextView are the same size, so I put the attribute android:layout_weight="1" in each.

When there is not text in the TextView everything works fine:

enter image description here

but when I add the text corresponding to each TextView, the size of Textview change, not keep the weight and resize the LinearLayout

enter image description here

This is only part of the design of my Activity, so I recreated it in a new xml to see if the error persists. And I have noticed that this only happens when the LinearLayout is inside of a HorizontalScrollView

When it is not, I get the expected result:

enter image description here

My xml:

<HorizontalScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true">

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

        <TextView
            android:singleLine="true"
            android:text="LONG TEXT"
            android:layout_marginBottom="1dp"
            android:layout_marginTop="1dp"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:background="#009FDC"
            android:gravity="center"
            android:padding="5dp"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="#fff"
            android:textStyle="bold"
            android:typeface="serif" />

        <TextView
            android:singleLine="true"
            android:layout_marginLeft="1dp"
            android:text="LONG TEXT"
            android:layout_marginBottom="1dp"
            android:layout_marginTop="1dp"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:background="#009FDC"
            android:gravity="center"
            android:padding="5dp"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="#fff"
            android:textStyle="bold"
            android:typeface="serif" />

        <TextView
            android:singleLine="true"
            android:layout_marginLeft="1dp"
            android:text="LONG TEXT"
            android:layout_marginBottom="1dp"
            android:layout_marginTop="1dp"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:background="#009FDC"
            android:gravity="center"
            android:padding="5dp"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="#fff"
            android:textStyle="bold"
            android:typeface="serif" />

        <TextView
            android:singleLine="true"
            android:layout_marginLeft="1dp"
            android:text="LONG TEXT"
            android:layout_marginBottom="1dp"
            android:layout_marginTop="1dp"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:background="#009FDC"
            android:gravity="center"
            android:padding="5dp"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="#fff"
            android:textStyle="bold"
            android:typeface="serif" />

        <TextView
            android:singleLine="true"
            android:layout_marginLeft="1dp"
            android:text="LONG TEXT"
            android:layout_marginBottom="1dp"
            android:layout_marginTop="1dp"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:background="#009FDC"
            android:gravity="center"
            android:padding="5dp"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="#fff"
            android:textStyle="bold"
            android:typeface="serif" />

        <TextView
            android:singleLine="true"
            android:layout_marginLeft="1dp"
            android:text="LONG TEXT"
            android:layout_marginBottom="1dp"
            android:layout_marginTop="1dp"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:background="#009FDC"
            android:gravity="center"
            android:padding="5dp"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="#fff"
            android:textStyle="bold"
            android:typeface="serif" />

        <TextView
            android:singleLine="true"
            android:layout_marginLeft="1dp"
            android:text="LONG TEXT"
            android:layout_marginBottom="1dp"
            android:layout_marginTop="1dp"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:background="#009FDC"
            android:gravity="center"
            android:padding="5dp"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="#fff"
            android:textStyle="bold"
            android:typeface="serif" />

    </LinearLayout>
</HorizontalScrollView>

Anyone know how to fix this? thanks in advance.

Oscar Méndez
  • 798
  • 1
  • 10
  • 33

3 Answers3

1

try this i tried with table layout

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true" >

    <TableLayout
        android:id="@+id/table_dashboared"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:stretchColumns="1" >

        <TableRow
            android:id="@+id/tablerow_header"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:weightSum="7" >

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="#009FDC"
                android:gravity="center"
                android:padding="5dp"
                android:singleLine="true"
                android:text="LONG TEXT"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textColor="#fff"
                android:textSize="12sp"
                android:textStyle="bold"
                android:typeface="serif" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="1dp"
                android:layout_weight="1"
                android:background="#009FDC"
                android:gravity="center"
                android:padding="5dp"
                android:singleLine="true"
                android:text="LONG TEXT"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textColor="#fff"
                android:textStyle="bold"
                android:typeface="serif" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="1dp"
                android:layout_weight="1"
                android:background="#009FDC"
                android:gravity="center"
                android:padding="5dp"
                android:singleLine="true"
                android:text="LONG TEXT"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textColor="#fff"
                android:textStyle="bold"
                android:typeface="serif" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="1dp"
                android:layout_weight="1"
                android:background="#009FDC"
                android:gravity="center"
                android:padding="5dp"
                android:singleLine="true"
                android:text="LONG TEXT"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textColor="#fff"
                android:textStyle="bold"
                android:typeface="serif" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="1dp"
                android:layout_weight="1"
                android:background="#009FDC"
                android:gravity="center"
                android:padding="5dp"
                android:singleLine="true"
                android:text="LONG TEXT"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textColor="#fff"
                android:textStyle="bold"
                android:typeface="serif" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="1dp"
                android:layout_weight="1"
                android:background="#009FDC"
                android:gravity="center"
                android:padding="5dp"
                android:singleLine="true"
                android:text="LONG TEXT"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textColor="#fff"
                android:textStyle="bold"
                android:typeface="serif" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="1dp"
                android:layout_weight="1"
                android:background="#009FDC"
                android:gravity="center"
                android:padding="5dp"
                android:singleLine="true"
                android:text="LONG TEXT"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textColor="#fff"
                android:textStyle="bold"
                android:typeface="serif" />
        </TableRow>
    </TableLayout>

</HorizontalScrollView>
M S Gadag
  • 2,011
  • 1
  • 10
  • 15
0

I have changed your xml a bit. Try this and let me know the result

<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">

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

    <TextView
        android:singleLine="true"
        android:text="LONG TEXT"
        android:layout_marginBottom="1dp"
        android:layout_marginTop="1dp"
        android:layout_width="fill_parent"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:background="#009FDC"
        android:gravity="center"
        android:padding="5dp"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#fff"
        android:textStyle="bold"
        android:typeface="serif" />

    <TextView
        android:singleLine="true"
        android:layout_marginLeft="1dp"
        android:text="LONG TEXT"
        android:layout_marginBottom="1dp"
        android:layout_marginTop="1dp"
        android:layout_width="fill_parent"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:background="#009FDC"
        android:gravity="center"
        android:padding="5dp"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#fff"
        android:textStyle="bold"
        android:typeface="serif" />

    <TextView
        android:singleLine="true"
        android:layout_marginLeft="1dp"
        android:text="LONG TEXT"
        android:layout_marginBottom="1dp"
        android:layout_marginTop="1dp"
        android:layout_width="fill_parent"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:background="#009FDC"
        android:gravity="center"
        android:padding="5dp"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#fff"
        android:textStyle="bold"
        android:typeface="serif" />

    <TextView
        android:singleLine="true"
        android:layout_marginLeft="1dp"
        android:text="LONG TEXT"
        android:layout_marginBottom="1dp"
        android:layout_marginTop="1dp"
        android:layout_width="fill_parent"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:background="#009FDC"
        android:gravity="center"
        android:padding="5dp"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#fff"
        android:textStyle="bold"
        android:typeface="serif" />

    <TextView
        android:singleLine="true"
        android:layout_marginLeft="1dp"
        android:text="LONG TEXT"
        android:layout_marginBottom="1dp"
        android:layout_marginTop="1dp"
       android:layout_width="fill_parent"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:background="#009FDC"
        android:gravity="center"
        android:padding="5dp"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#fff"
        android:textStyle="bold"
        android:typeface="serif" />

    <TextView
        android:singleLine="true"
        android:layout_marginLeft="1dp"
        android:text="LONG TEXT"
        android:layout_marginBottom="1dp"
        android:layout_marginTop="1dp"
       android:layout_width="fill_parent"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:background="#009FDC"
        android:gravity="center"
        android:padding="5dp"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#fff"
        android:textStyle="bold"
        android:typeface="serif" />

    <TextView
        android:singleLine="true"
        android:layout_marginLeft="1dp"
        android:text="LONG TEXT"
        android:layout_marginBottom="1dp"
        android:layout_marginTop="1dp"
        android:layout_width="fill_parent"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:background="#009FDC"
        android:gravity="center"
        android:padding="5dp"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#fff"
        android:textStyle="bold"
        android:typeface="serif" />

</LinearLayout>

Praveena
  • 5,226
  • 1
  • 35
  • 50
0

Try this code:

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fillViewport="true" >

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:weightSum="7" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="1dp"
        android:layout_marginTop="1dp"
        android:layout_weight="1"
        android:background="#009FDC"
        android:gravity="center"
        android:padding="5dp"
        android:singleLine="true"
        android:text="LONG TEXTTTTTTTTTTTTTTTTTTTTTTTTTT"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#fff"
        android:textStyle="bold"
        android:typeface="serif" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="1dp"
        android:layout_marginLeft="1dp"
        android:layout_marginTop="1dp"
        android:layout_weight="1"
        android:background="#009FDC"
        android:gravity="center"
        android:padding="5dp"
        android:singleLine="true"
        android:text="LONG TEXT"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#fff"
        android:textStyle="bold"
        android:typeface="serif" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="1dp"
        android:layout_marginLeft="1dp"
        android:layout_marginTop="1dp"
        android:layout_weight="1"
        android:background="#009FDC"
        android:gravity="center"
        android:padding="5dp"
        android:singleLine="true"
        android:text="LONG TEXT"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#fff"
        android:textStyle="bold"
        android:typeface="serif" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="1dp"
        android:layout_marginLeft="1dp"
        android:layout_marginTop="1dp"
        android:layout_weight="1"
        android:background="#009FDC"
        android:gravity="center"
        android:padding="5dp"
        android:singleLine="true"
        android:text="LONG TEXT"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#fff"
        android:textStyle="bold"
        android:typeface="serif" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="1dp"
        android:layout_marginLeft="1dp"
        android:layout_marginTop="1dp"
        android:layout_weight="1"
        android:background="#009FDC"
        android:gravity="center"
        android:padding="5dp"
        android:singleLine="true"
        android:text="LONG TEXT"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#fff"
        android:textStyle="bold"
        android:typeface="serif" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="1dp"
        android:layout_marginLeft="1dp"
        android:layout_marginTop="1dp"
        android:layout_weight="1"
        android:background="#009FDC"
        android:gravity="center"
        android:padding="5dp"
        android:singleLine="true"
        android:text="LONG TEXT"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#fff"
        android:textStyle="bold"
        android:typeface="serif" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="1dp"
        android:layout_marginLeft="1dp"
        android:layout_marginTop="1dp"
        android:layout_weight="1"
        android:background="#009FDC"
        android:gravity="center"
        android:padding="5dp"
        android:singleLine="true"
        android:text="LONG TEXT"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#fff"
        android:textStyle="bold"
        android:typeface="serif" />
</LinearLayout>
</HorizontalScrollView>

enter image description here

Iman Marashi
  • 4,677
  • 33
  • 48