-1

Trying to get a layout the way I want it. It contains a ListView and a LinearLayout which contains two TextViews.

The problem is with the ListView taking up the entire screen if it has more items then fits in one screen. In that case it doesn't show the View underneath it.

If I set the ListView to layout_weight=1 with layout_height=0dp then the View underneath it goes all the way to the bottom because the ListView fills the screen if not enough items are on it.

So how do I get a ListView with a View directly underneath it, so without setting it to the bottom of the screen?

As requested (but if the listview doesn't fit on one screen you don't get to see the view below listview):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <ListView
        android:id="@+id/list_bookings"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal" />

    <LinearLayout
        android:id="@+id/expected_cost_linearLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/padding_regular">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:focusable="false"
            android:text="@string/expected_cost_label"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/expected_cost_textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="..."
            android:focusable="false" />
    </LinearLayout>

</LinearLayout>
Rekijan Kileren
  • 183
  • 1
  • 3
  • 16
  • Add margin bottom to you listview to subtract with the hieght with your Linearlayout at the bottom. For instance if the LinearLayout is of 50dp hieght at the bottom. Give Margin bottom as 50dp to listview – Nitin Mesta Dec 07 '15 at 10:58
  • Use a relative layout and place that view under listview by specifying android:layout_below property – aiRbornE Dec 07 '15 at 11:00
  • @RAP Code is irrelevant in this case, its a listview with a linearlayout and two textviews, the actual parameters to those is what I need to know. – Rekijan Kileren Dec 07 '15 at 11:52
  • @aiRbornE the problem with that is that if there are more items in the listview then fits on one screen the view below the listview isn't displayed. This is because, in such a case, the listview sets its height to the whole screen, not leaving room for the view. – Rekijan Kileren Dec 07 '15 at 11:54
  • @NitinMesta I tried it but the problem remains that if the listview is too big for one screen its sets its height to be the entire screen. The margin then only reduces the effective height, the view beneath still doesn't appear. – Rekijan Kileren Dec 07 '15 at 11:57
  • Can you put your layout file at least? – Nitin Mesta Dec 07 '15 at 13:14
  • @NitinMesta I added the layout code. – Rekijan Kileren Dec 07 '15 at 13:24

2 Answers2

1
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <ListView
        android:id="@+id/list_bookings"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorPrimaryDark"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="50dp" />

    <LinearLayout
        android:id="@+id/expected_cost_linearLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:focusable="false"
            android:text="expected_cost_label"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/expected_cost_textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="..."
            android:focusable="false" />
    </LinearLayout>

</RelativeLayout>

UPDATED ANSWER

Use this layout file

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    xmlns:android="http://schemas.android.com/apk/res/android" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <ListView
        android:id="@+id/list_bookings"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimaryDark"
        android:layout_gravity="center_horizontal"
         />

    <LinearLayout
        android:id="@+id/expected_cost_linearLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:focusable="false"
            android:text="expected_cost_label"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/expected_cost_textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="..."
            android:focusable="false" />
    </LinearLayout>

</LinearLayout>
    </ScrollView>

// And in your layout file call this method after setting adapter to your listview. This code was taken from https://stackoverflow.com/a/19311197/2793134

public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null)
            return;

        int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED);
        int totalHeight = 0;
        View view = null;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            view = listAdapter.getView(i, view, listView);
            if (i == 0)
                view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, AbsListView.LayoutParams.WRAP_CONTENT));

            view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
            totalHeight += view.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }

Hope this helps. Cheers

Community
  • 1
  • 1
Nitin Mesta
  • 1,504
  • 19
  • 32
  • Thanks for your help but this doesn't do what I want. You are aligning the View below the ListView to the bottom of the screen. I only want that behaviour when the ListView is bigger then one screen. But when it is not, I want the View to be directly below the ListView. – Rekijan Kileren Dec 08 '15 at 07:41
  • You can use scroll view as the root tag with fillViewPort set to true and put your content that is ListView and Linear Layout one below the other using linear layout – Nitin Mesta Dec 08 '15 at 07:46
  • This might work but adjusting the layout in your code is something that feels 'hacky' and 'dirty'. But thanks for thinking with me, I realized I was overthinking this problem and have come to a solution myself. – Rekijan Kileren Dec 10 '15 at 08:19
0

I was thinking way too complicated. I ended up making a ListItem that could go in the adapter with the layout I wanted. This way it became part of the ListView and its position can be controlled to be the bottom one. And because it is part of the ListView it isn't getting obfuscated by the ListView like a view below a ListView gets when there are too many items.

Rekijan Kileren
  • 183
  • 1
  • 3
  • 16