Advantage of using single TableLayout instead of multiple LinearLayouts as TableRow extends LinearLayout only
Asked Answered
M

3

7

My listview item consists of three columns and three rows.I used TableLayout for it.The space between columns was not uniform but i managed by setting margins.Now the layout looks perfect.

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/dp18"
    android:paddingLeft="@dimen/dp16"
    android:paddingTop="@dimen/dp18"
    android:stretchColumns="0,1,2">
    <TableRow>
        ..
    </TableRow>
    <TableRow>
        ..
    </TableRow>
    <TableRow>
        ..
    </TableRow>
</TableLayout>

But I wonder what is the ideal approach? What will be the difference in terms of optimization and performance if I have used four LinearLayouts (one outer horizontal linearlayout and three internal vertical linearlayout for three columns). As TableRow extends LinearLayout then indirectly I was using LinearLayout only.

Then, what is the Advantage of using single TableLayout instead of multiple LinearLayouts as TableRow extends LinearLayout only?

Metronome answered 15/8, 2016 at 16:5 Comment(3)
Did you think about RecyclerView? I think that it will give you most flexibility and best performances, especially if you have long list.Fiscus
@Shaishav I was just waiting for any more answer or comments.. anyway I am going to accept it..Metronome
@Vladmir Jovanovic Yeah Now I am using recyclerview only..Metronome
C
9

TableLayout is advantageous or LinearLayout is depends on the application and layout you require.

Well, you can achieve everything by LinearLayout, which you can get from TableLayout as it extends LinearLayout.

So, what’s the purpose of TableLayout?

Obviously, it gives more flexibility as you can arrange layout children into rows and columns. So, view looks more organize. Achieving such things by LinearLayout and its property like weight, orientation, margin, padding etc. is really tiresome.

Second difference is the methods setColumnShrinkable(), setColumnStretchable(), setColumnCollapsed() etc. introduce in TableLayout. Look at Documentation.

Again, these methods help to organize view and you can span columns /cell, as you can in HTML.

Example

Where TableLayout is useful compare to LinearLayout.

Consider a scenario when you want something like below: Full Detail Question

|img|leftText|rightText|                ||(end of screen)
|img|leftTextMedium|rightText|          ||
|img|leftTextTooLongSoTrunc...|rightText||

rightText must always appear on screen next to leftText no matter size of leftText. You can’t achieve something just by LinearLayout XML file (If you use weight property than it will add space between left and right text which is not desire output.)

You can achieve this easily by using android:shrinkColumns in TableLayout (check accepted answer in above mention question) but for LinearLayout you have to do some programming, XML won't works alone. Look here for code

I suppose you are now clear with the difference between above two Layouts.

Keep in mind that performance wise LinearLayout is better because TableLayout render more UI views and extends more methods. So, you should use TableLayout only when you require Row-Column behavior.

Confidante answered 24/8, 2016 at 12:17 Comment(0)
R
1

I think it is not about performance or optimization, but about convenient way to create tables. TableLayout and TableRow is like every utils class. It is not necessary, but nice to have as it is providing several useful features (like layout_column).

If you will make your own Table with a horrible utils part then It will propably work slower, but it is not a game, the difference will be insignificant.

TLDR: Advantage is convenience of using TableLayout instead of multiple LinearLayout

Rothman answered 21/8, 2016 at 12:26 Comment(0)
V
1

This is depends on what your objective. Yes, TableLayout extends from LinearLayout. But in TableLayout has more features which LinearLayout doesn't have. So if your requirements meet some exiting implemented functions, you can reduce your code and use it directly.

TableLayout

The width of a column is defined by the row with the widest cell in that column. However, a TableLayout can specify certain columns as shrinkable or stretchable by calling setColumnShrinkable() or setColumnStretchable(). If marked as shrinkable, the column width can be shrunk to fit the table into its parent object. If marked as stretchable, it can expand in width to fit any extra space. The total width of the table is defined by its parent container. It is important to remember that a column can be both shrinkable and stretchable. In such a situation, the column will change its size to always use up the available space, but never more. Finally, you can hide a column by calling setColumnCollapsed().

To sum up, if you could done your design layout without using TabLayout, this also a good way to reduce the UI render performance as TabLayout included some feature or functions that you won't use.

Veritable answered 24/8, 2016 at 10:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.