Jean-Philippe Roy
Jean-Philippe Roy

Reputation: 4812

Android - Weird TextView Behavior

I'm a bit puzzled, I'm currently designing an Activity's UI and two of the TextView widget in the said UI are acting a bit weird.

I have set the background to android:background="#CCCCCC" in order to observe the behavior more easily, this will eventually be removed when I obtain the look I'm aiming for.

When both the TextView are empty, the behavior is what I expect from my UI :

enter image description here

But when the TextView are filled with different length strings, the width gets changed :

enter image description here

Here's the code attached to my UI's row.

        <TableRow android:weightSum="2" >
            <TextView
                android:id="@+id/tvDate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="#CCCCCC"
                android:gravity="center_vertical|center_horizontal"
                android:padding="4dp"
                android:textColor="#000000" />

            <View
                android:layout_width="8dp"
                android:layout_height="fill_parent" />

            <TextView
                android:id="@+id/tvTime"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="#CCCCCC"
                android:gravity="center_vertical|center_horizontal"
                android:padding="4dp"
                android:textColor="#000000" />
        </TableRow>

So far I've tried to change the android:layout_width parameter to a plethora of values with different android:padding and android:layout_weight combination. But nothing gave me the desired effect (which is to have the two TextView occupy the same ammount of space).

I'm far from an Android XML expert so any pointers would be greatly appreciated.

Upvotes: 0

Views: 243

Answers (1)

Matt M
Matt M

Reputation: 814

I would suggest using a LinearLayout.

<LinearLayout
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:weightSum="1.0"
     android:orientation="horizontal">
<TextView
     android:id="@+id/tvDate"
     android:layout_width="0dp"
     android:layout_height="wrap_content"
     android:layout_weight="0.5"
     android:background="#CCCCCC"
     android:gravity="center_vertical|center_horizontal"
     android:paddingRight="4dp"
     android:textColor="#000000" />
<TextView
     android:id="@+id/tvTime"
     android:layout_width="0dp"
     android:layout_height="wrap_content"
     android:layout_weight="0.5"
     android:background="#CCCCCC"
     android:gravity="center_vertical|center_horizontal"
     android:paddingLeft="4dp"
     android:textColor="#000000" />
</LinearLayout>

<LinearLayout
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:weightSum="1.0"
     android:orientation="horizontal">
<Button
     android:id="@+id/buttonDate"
     android:layout_width="0dp"
     android:layout_height="wrap_content"
     android:layout_weight="0.5"
     android:gravity="center_vertical|center_horizontal"
     android:paddingRight="4dp"
     android:text="Pick Date" />
<Button
     android:id="@+id/buttonTime"
     android:layout_width="0dp"
     android:layout_height="wrap_content"
     android:layout_weight="0.5"
     android:gravity="center_vertical|center_horizontal"
     android:paddingLeft="4dp"
     android:text="Pick Time" />
</LinearLayout>

Upvotes: 2

Related Questions