Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create TextViews with equal width in a row

Tags:

android

I am trying to create a layout with three textviews beside each other. The textviews are of different length.

All I want to do is to arrange them so that they always have equal layout_width irrespective of the length of their text.

Approaches I tried:

1) I tried using linear_layout and setting the weight of textviews to be 1 but that does not do it since it applies only for the remaining width after all textViews are positioned.

2) I also tried using table layout and stretching all the columns by using stretchColumns="*". but that also allocates the required width to the length of the texts and only then does it stretch the columns.

3) This is a hack but this is not working either. I tried to have a linear layout inside a relative layout with three textviews to have no text but force them to be of equal widths by setting their weights. This works but then I want my next views which are in the relative layout to align themselves to the right and left edges of the views within the linear layout. But this does not work. Here is my layout that I used for this.

        <RelativeLayout
            android:layout_width="fill_parent" android:layout_height="fill_parent" >
            <!-- This is a hack to have all three textviews in a row to obtain equal width -->
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >
                <TextView android:id="@+id/tv_ref1"
                    android:layout_width="0dp" android:layout_height="wrap_content"
                    android:text="My"
                    android:layout_weight="1.0"
                    android:gravity="center" />
                <TextView android:id="@+id/tv_ref2"
                    android:layout_width="0dp" android:layout_height="wrap_content"
                    android:text="My"
                    android:layout_weight="1.0"
                    android:gravity="center" />
                <TextView android:id="@+id/tv_ref3"
                    android:layout_width="0dp" android:layout_height="wrap_content"
                    android:text="My ghghhfghjjghjkkhgfhfghfjhjh"
                    android:layout_weight="1.0"
                    android:gravity="center" />
            </LinearLayout>


            <TextView
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:layout_below="@id/tv_ref1"
                android:gravity="center"
                android:text="Text Text Text"
                android:textColor="@color/white" />
            <TextView
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:layout_below="@id/tv_ref1"
                android:gravity="center_horizontal"
                android:text="Med"
                android:textColor="@color/white" />
            <TextView
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:layout_below="@id/tv_ref1"
                android:gravity="center"
                android:text="Long Text wtwy yu hjsyi"
                android:textColor="@color/white" />

        </RelativeLayout>

Please let me know what I might be doing wrong or how I can force my textviews in a row to have equal widths.

Thank you.

like image 924
achie Avatar asked Jun 23 '11 07:06

achie


1 Answers

Setting layout_width to "fill_parent" and layout_weight to "1" for all your TextEdits forces them to have equal width

like image 158
Mykhailo Gaidai Avatar answered Nov 19 '22 00:11

Mykhailo Gaidai