Android Addicted Apps Creator

August 2, 2012

Tip #1 Make TableRow items of same width

Filed under: Android,Tips — alinberce @ 15:56

I usually don’t use TableLayout, but when I do I get annoyed by it. To be honest I find it way more flexible to use nested LinearLayouts as it will be easier in the future to update the design. I had this page where I needed a grid of 3×3 buttons. Of course, using LinearLayout over and over again wouldn’t have been too pleasant, so I went for the TableLayout. The curious thing was that even if I added a layout_weight=”1” for each button, in the end their width varied based on their text. After saying that TableLayout sucks… well… ergh… found the solution. Set layout_width=”0dip”. Simple yet effective.

So, to recap: if you want views from a TableRow to have the same width you need to set two properties, for each view:

 android:layout_width="0dip"
 android:layout_weight="1"

So in the end it would look something like:

 <TableLayout
       android:layout_width="fill_parent"
        android:layout_height="fill_parent"" >

        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1" >

            <Button
                android:layout_width="0dip"
                android:layout_height="fill_parent"
                android:layout_marginLeft="5dip"
                android:layout_marginRight="5dip"
                android:layout_weight="1"
                android:text="I am equal with my brother button" />

             <Button
                android:layout_width="0dip"
                android:layout_height="fill_parent"
                android:layout_marginLeft="5dip"
                android:layout_marginRight="5dip"
                android:layout_weight="1"
                android:text="I am equal with my brother button" />
        </TableRow>
...
Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: