William Kinaan
William Kinaan

Reputation: 28799

Best practice for layout two textviews on a row

i want to have on my xml layout many textviews , each two of them on a row, i make it as this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Name: " />

        <TextView
            android:id="@+id/playertvName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/red"
            android:textSize="20dp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Nationality: " />

        <TextView
            android:id="@+id/playertvNationality"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/red"
            android:textSize="20dp" />
    </LinearLayout>

</LinearLayout>

but it seems not a good way to keep creating a new linearlayout for each two textviews, is there another way please?thanks

Upvotes: 3

Views: 412

Answers (4)

jaibatrik
jaibatrik

Reputation: 7523

I believe, for the purpose you are using the Layout (a form possibly), the way you have developed it is quite OK. I don't think a GridView or any AdapterView would suit your requirement in this case.

You can use LinearLayout with proper weights or RelativeLayout.

Upvotes: 2

kumar_android
kumar_android

Reputation: 2263

Use one Relative layout and play with android:layout_toLeftOf="", android:layout_toRightOf="" android:layout_below="" android:layout_above=""

Upvotes: 0

jeet
jeet

Reputation: 29199

You can use TableLayout or GridView, as an alternate of LinearLayout.

  1. TableLayout will allow you to allign text views in table format, and I would recommend TableLayout if number of textviews are very small and fixed.

  2. GridView allows you to layout your components in Grid fasion, its an adapterview, which means, by GridView we can re-use Views. It increases complexity upto some extent, so Its useful when no of textviews, is large.

Upvotes: 1

Andrii Chernenko
Andrii Chernenko

Reputation: 10194

Try GridLayout. Here's official Google doc, and here's the example.

Upvotes: 3

Related Questions