3

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

William Kinaan
  • 28,059
  • 20
  • 85
  • 118

4 Answers4

3

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

Andrii Chernenko
  • 9,873
  • 7
  • 71
  • 89
  • there is no GridLayout on my android , is it wrong from me or i have to install something? – William Kinaan Dec 08 '12 at 08:41
  • @totti roma It was introduced in API 14 (Android 4.0). But there's a backport in v7 support library, so you can use it for API 7+ (Android 2.1+). Check out this question: http://stackoverflow.com/questions/10278358/grid-layout-support-in-android-api-10 – Andrii Chernenko Dec 08 '12 at 08:44
  • first +1 for u, second where should i download this library? i am using 2.2 emulator on eclipse , third if i works with me good , when i move the adk to my phone will that library move with it? or i should to download it to photo too ? – William Kinaan Dec 08 '12 at 08:54
  • Check out this doc: http://developer.android.com/tools/extras/support-library.html – Andrii Chernenko Dec 08 '12 at 09:01
  • everything is installed when i opened the sdk , please would you just tell me where to find griplayout ? – William Kinaan Dec 08 '12 at 09:14
  • @totti roma if you have already downloaded support library via SDK Manager, next step is to add it to your project. How do you do that depends on IDE you use. `*.jar` with library can be found in `/extras/android/support/v4/android-support-v7.jar`. After you add library to your project, you should be able to use it like ay other (`LinearLayout`, `RelativeLayout` etc.) – Andrii Chernenko Dec 08 '12 at 16:17
2

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.

jaibatrik
  • 6,770
  • 9
  • 33
  • 62
  • thanks , but why u don't thing that gridlayout is not good ? BTY i hope there is another solution without gridlayout because it seems i have to download the library :( – William Kinaan Dec 08 '12 at 08:56
  • Sorry, I mixed up in GridLayout and GridView. I think GridLayout is fine for your case and I have upvoted deville's answer now :) There's the official v7 compatibility package for GridLayout. If you don't want to use it still, RelativeLayout will be good. – jaibatrik Dec 08 '12 at 09:05
  • but how can i download griplayout? i opend the sdk and everything is installed – William Kinaan Dec 08 '12 at 09:13
  • when i built the project i set the target platform is 2.2, bty i have installed platform 4.3 , so if i changed the target platform to 4.3 and i works good , then when i move the apk to my phone (2.2) will it work or still i have to install those libraries to my phone? – William Kinaan Dec 08 '12 at 09:18
  • Try adding the support library. Right click on project, Android Tools > Add support library Then use – jaibatrik Dec 08 '12 at 09:20
  • it seems that i have installed griplayout because when i searched on my skd i found it , now how could i use it please? – William Kinaan Dec 08 '12 at 09:22
  • Use this in your layout file – jaibatrik Dec 08 '12 at 09:24
1

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.

jeet
  • 29,001
  • 6
  • 52
  • 53
0

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

kumar_android
  • 2,273
  • 1
  • 21
  • 30