Ruchir
Ruchir

Reputation: 1122

Problems in TableLayout for custom ListView in Android

In my Android app I'm retrieving data from server using JSON. To display that data, I'm using a custom ListView that has 3 static images and two TextViews. Data is displaying in ListView but in different manner. image1

I want output like this image. But my output list is different in which sometimes those numbers go missing from list. image2 Here after 104, numbers 105 and 106 is missing and then 110 is missing. I am not getting any idea why output is like this. Here's my xml layout code below:

XML Code

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:stretchColumns="0,1,2"
    android:shrinkColumns="1"
    android:layout_marginLeft="10dp">

    <TableRow
        android:layout_width="match_parent">
        <TextView
            android:id="@+id/SrNo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="5dp"
            android:text="SR NO."
            android:textColor="#000000"
            android:singleLine="true"
            android:layout_marginTop="10dp"/>

        <TextView
            android:id="@+id/Name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Expert Name Here."
            android:textColor="#000000"
            android:paddingBottom="5dp"
            android:gravity="center"
            android:layout_marginTop="10dp"/>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="right">

            <ImageView
                android:id="@+id/edit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@android:drawable/ic_menu_view"
                android:paddingBottom="5dp"
                android:layout_marginTop="5dp"/>

            <ImageView
                android:id="@+id/view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@android:drawable/ic_menu_edit"
                android:paddingBottom="5dp"
                android:layout_marginTop="5dp"/>

            <ImageView
                android:id="@+id/delete"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@android:drawable/ic_menu_delete"
                android:paddingBottom="5dp"
                android:layout_marginTop="5dp"/>

        </LinearLayout>

    </TableRow>
</TableLayout>

Upvotes: 2

Views: 360

Answers (1)

Onik
Onik

Reputation: 19969

You've almost been there within the discussion with @Sangeeta.

Since TableRow is inherited from LinearLayout the concept of weight is valid for it too, according to which the layout_width attribute has to be set to 0dp in order for a view to be spreaded horizontally.

The following needs to be changed:

  1. Remove android:stretchColumns="0,1,2"
  2. set android:layout_width="0dp" wherever layout_weight attribute is used.

(Note: I left only relevant attributes for the question)

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

<TableRow
    android:layout_width="match_parent">
    <TextView
        android:id="@+id/SrNo"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.25"/>

    <TextView
        android:id="@+id/Name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"/>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.25">

        <ImageView
            android:id="@+id/edit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        <ImageView
            android:id="@+id/view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        <ImageView
            android:id="@+id/delete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

    </LinearLayout>

</TableRow>
</TableLayout>

Upvotes: 1

Related Questions