exponentialFun
exponentialFun

Reputation: 225

xml vs doing programmatically

I am developing an app in android. the app should have listing of items. It will look somewhat as below : sample

The problem is I need to list around 40 items the above way. I can do that using relative layout in xml file. But it is getting too long. For example for only creating 3 items my code looks huge like below(let alone 40) :

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

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/tvb" >

        <ImageView
            android:id="@+id/image_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="3dp"
            android:adjustViewBounds="true"
            android:maxHeight="60dp"
            android:maxWidth="100dp"
            android:scaleType="fitCenter"
            android:src="@drawable/tomato" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/image_view"
            android:text="Tomato"
            android:textColor="#000000"
            android:textSize="25dp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="20gm"
            android:textColor="#000000"
            android:textSize="20dp"
            android:textStyle="bold" />
    </RelativeLayout>
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/tvb" >

        <ImageView
            android:id="@+id/image_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="3dp"
            android:adjustViewBounds="true"
            android:maxHeight="60dp"
            android:maxWidth="100dp"
            android:scaleType="fitCenter"
            android:src="@drawable/begun" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/image_view"
            android:text="Begun"
            android:textColor="#000000"
            android:textSize="25dp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="45gm"
            android:textColor="#000000"
            android:textSize="20dp"
            android:textStyle="bold" />
    </RelativeLayout>
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/tvb" >

        <ImageView
            android:id="@+id/image_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="3dp"
            android:adjustViewBounds="true"
           android:maxHeight="60dp"
            android:maxWidth="100dp"
            android:scaleType="fitCenter"
            android:src="@drawable/potol" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/image_view"
            android:text="Potol"
            android:textColor="#000000"
            android:textSize="25dp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="35gm"
            android:textColor="#000000"
            android:textSize="20dp"
            android:textStyle="bold" />
    </RelativeLayout>

</LinearLayout>

However I can copy and paste it and do it for rest 37 items. But is it really efficient. That means getting xml file too long ... is it really efficient... Again doing it in java may slow down the code. Cause I need Add image for every list item too.

I am expecting a good guideline. Can anyone suggest ?

Upvotes: 0

Views: 220

Answers (2)

MikeKeepsOnShine
MikeKeepsOnShine

Reputation: 1750

Use ListView with a ListViewAdapter. This is a good and complete tutorial http://www.vogella.com/articles/AndroidListView/article.html

Upvotes: 2

ssantos
ssantos

Reputation: 16526

You can create a separate layout xml for a row and then include it in your main layout as many times as you need.-

_row.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/tvb" >

    <ImageView
        android:id="@+id/image_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="3dp"
        android:adjustViewBounds="true"
        android:maxHeight="60dp"
        android:maxWidth="100dp"
        android:scaleType="fitCenter"
        android:src="@drawable/tomato" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/image_view"
        android:text="Tomato"
        android:textColor="#000000"
        android:textSize="25dp"
        android:textStyle="bold" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="20gm"
        android:textColor="#000000"
        android:textSize="20dp"
        android:textStyle="bold" />
</RelativeLayout>

main_layout.xml

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

    <include
        android:id="@+id/row1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        layout="@layout/_row" />

    <include
        android:id="@+id/row2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        layout="@layout/_row" />

    ...

</LinearLayout>

Notice that for a dynamic list it'd be better to go for a ListView, or if your list is 'static', at least you would need to use a ScrollView.

Upvotes: 0

Related Questions