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>
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>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With