AndreaNobili AndreaNobili - 5 months ago 11
Java Question

Is it better use an horizontal LinearLayout or TableLayout?

I am absolutely new in Android development and I have the following doubt developing my first app.

I have to horizontally divide my layout into 2 sections having the same size (half screen has to be occupied by the left section and half screen by the right section).

These 2 sections have to contain some text images.

What is the best strategy to do it? Using a horizontal

LinearLayout
or something like a
TableLayout
?

Answer

The main purpose of TableLayout is you can easily divide your layout into multiple rows and columns.

For such a simple layout you could just use a horizontal LinearLayout that contains two layouts for your left and right content.

For example:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <!-- left content -->

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <!-- right content -->

    </RelativeLayout>
</LinearLayout>

Using the layout_weight attribute instead of defining a layout_width ensures that that the left and right layouts will divide the screen equally on all devices/screen orientations.

Equal layout_weight values mean the layouts are equally "important", so they will get equal space from the parent LinearLayout.

Check this link out for more info on LinearLayout and the layout_weight attribute.