Search code examples
androidandroid-layoutlistviewandroid-tablelayout

TableLayout below Listview not displaying


I want to show a TableLayout below a Listview (where the the no. of items are dynamically added). I referred to a similar question before, but it didn't work for me. I am using android:layout_below to set the TableLayout below the Listview but it still doesn't work, I would appreciate anyone guiding me on how to do this.

Here is my code:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />


    </android.support.design.widget.AppBarLayout>

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:clipToPadding="false"
        android:divider="@color/list_divider"
        android:dividerHeight="10.0sp"
        android:listSelector="@drawable/list_row_selector"
        android:paddingBottom="100dip"
        android:paddingTop="70dip" />

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/list"
        android:layout_weight="1">

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="20dp">

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Total Items"
                android:textAppearance="?android:attr/textAppearanceLarge" />

            <TextView
                android:id="@+id/textView11"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="idvalue_text"
                android:textAppearance="?android:attr/textAppearanceLarge" />

        </TableRow>

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="20dp">

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Total Cost"
                android:textAppearance="?android:attr/textAppearanceLarge" />

            <TextView
                android:id="@+id/textView21"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="idvalue_text"
                android:textAppearance="?android:attr/textAppearanceLarge" />

        </TableRow>

    </TableLayout>
</android.support.design.widget.CoordinatorLayout>

Solution

  • Okay. So as per our discussion in the comments section on taking a look at your code, it seems you want to have both the ListView and the TableLayout to have equal height (because you included layout_weight to both of them).

    So in order to produce your desired output, do the following.

    1. Use a RelativeLayout as the Parent Layout.

    2. Add a LinearLayout with weightSum = 2 and orientation=vertical, and put the ListView and the TableLayout inside it, where both of their height = 0dp and the layout_weight = 1.

    Here's a sample snippet for your reference:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
    
        </android.support.design.widget.AppBarLayout>
    
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:weightSum="2">
    
            <ListView
                android:id="@+id/list"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1" />
    
            <TableLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_below="@id/list"
                android:layout_weight="1">
    
            </TableLayout>
    
        </LinearLayout>
    
    </RelativeLayout>
    

    Doing so would result to something like this:

    enter image description here

    PS: This is just the display when checking the layout in the Android Studio preview, not when the code is running.