Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to limit the height of a ListView within a GridLayout?

I have a GridLayout containing a ListView and a TextView. When items are added to the ListView it will grow in size and push the TextView below it out of the screen. I thought that by setting layout_gravity="fill" on the ListView it would not grow at all but rather take up all the available space.

How do I configure the ListView to take up the available space instead of growing and pushing views below it out of the screen?

I am looking for a solution using a GridLayout. I am aware that in my case a LinearLayout would work aswell. However this is just a minimal test case to illustrate my problem.

I do not want to set the height programmatically, if possible.

<android.support.v7.widget.GridLayout 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"
    app:columnCount="1"
    app:useDefaultMargins="true" >

    <ListView
        app:layout_gravity="fill" />

    <TextView
        android:text="TextView" />

</android.support.v7.widget.GridLayout>
like image 356
Micha Wiedenmann Avatar asked Oct 08 '14 17:10

Micha Wiedenmann


2 Answers

This answer extends my comments to the question and attempts to explain why this cannot be done with XML via GridLayout's attributes.

Adding GridLayout to the framework allowed solving a range of drawbacks regarding nested layouts, one of wich was "inability to control alignment along both horizontal and vertical axes simultaneously" as described in the article New Layout Widgets: Space and GridLayout by Philip Milne.

This alignment control is performed by layout_gravity parameter whose description states that the parameter "specifies how a component should be placed in its group of cells", (NOT within a root layout). Basically, it's the same as that of LinearLayout, which also mentioned in the article: "Alignment/gravity also works just like gravity in LinearLayout..." And as we know, for LinearLayouts, once a view has taken the whole height of the screen (like ListView in your case), the views below it go behind the screen if the layout oriention was set to "vertical".

If we look at the GridLayout.LayoutParams documentation we won't find any parameter which would allow a component within a cell group to stick to a certain position of the parent layout and stay at the position independent of the size of other components within the same group; similar to the layout_align[...] parameters of a RelativeLayout.

My best guess about the absence of this kind of parameters in GridLayout is better alignment control compare to nested layouts.

I'm sure you know a solution(s) for your problem using an approach other than GridLayout's attributes. I'll just suggest one to make the answer look accomplished:

<GridLayout
    ... >

    <RelativeLayout
       ... >

        <TextView
            ...
            android:id="@+id/tv"
            android:layout_alignParentBottom="true" />

        <ListView
            ...
            android:layout_above="@id/tv">
         </ListView>

    </RelativeLayout>

</GridLayout>
like image 186
Onik Avatar answered Oct 06 '22 04:10

Onik


You can programmatically set the height of the ListView as follows:

ListView listView = (ListView) findViewById(R.id.listview);
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) listView.getLayoutParams();
layoutParams.height = listHeight;
listView.setLayoutParams(layoutParams);
listView.setAdapter(listAdapter);

However, I can't think of a way to make this scale well for multiple devices.

like image 22
varsha-venkatesh Avatar answered Oct 06 '22 03:10

varsha-venkatesh