Android Add Views or View Groups Below a ListView or GridView

You might be thinking showing up a View like TextView, ImageView, EditText, Button, etc. or a ViewGroup (wrapping other Views) like RelativeLayout or LinearLayout below a ListView/GridView must be easy. It’s not. Not in Android atleast.

The deal is, ListView and GridView (and maybe some other view groups that I can’t think of) are consumers of space. So as long as they’re contained in a small space that fits into the device’s screen and has enough area beneath, any View elements put in the layout file or added programatically after the ListView will surely present itself. But as soon as the ListView/GridView extends beyond the edges they’ll end up taking all the space pushing off the Views under and making them magically disappear.

What's the one thing every developer wants? More screens! Enhance your coding experience with an external monitor to increase screen real estate.

Solutions

There are a couple of solutions to this problem which are more of hacks or workarounds than proper fixes. Let’s discuss each of them.

android:layout_alignParentBottom=”true”

Given that the mutual parent is a RelativeLayout, this attribute (android:layout_alignParentBottom="true") on the View/ViewGroup underneath the ListView/GridView will push it at the bottom. As a result the item gets anchored (fixed/sticky) at the bottom (think of whatsapp’s attached chat box at the bottom whereas the chat messages are a list).

The ListView/GridView should have the android:layout_above property set to the ID of the View that’s being pushed below. It is important to remember not to do this the other way round, i.e., have android:layout_below on the View with the ListView/GridView ID as it may not behave as expected (strange Android behaviour, sorry). It is always recommended to set above on a ListView/GridView than set below on the subsequent item.

Note: This solution sticks the View rather than putting it after the ListView/GridView which means if the list or grid have only 2 children then the ensuing View/ViewGroup will stick at the bottom. If they have 20 children even then you’ll have the stickiness in the user interface.

android:layout_weight=”1″

If the mutual parent is LinearLayout then android:layout_weight="1" on the ListView/GridView will leave some space at the bottom where you can place your subsequent View/ViewGroup. This is going to be a sticky solution again (similar to the previous one).

ListView addFooterView() and addHeaderView()

The ListView class has two methods:

  • addFooterView() – This method can take in a View object to show up at the end of the list (like an extra list item). This is sort of what we might want if not an anchored View at the bottom. We can call this method multiple times adding multiple views. You could have a layout file, inflate that and pass it on to this method as demonstrated here.
  • addHeaderView() – Equivalent to the previous method but for headers.

As for GridView, there’s no such methods hence you’ll have to write some custom code/plugins or scout for and use some readymade (open-source) ones.

Maybe I’m missing something, but cannot see why such a behaviour. I think it would have made a lot more sense to easily allow us to show up content formed by Views even after Lists and Grids right into a single layout file.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Author: Rishabh

Rishabh is a full stack web and mobile developer from India. Follow me on Twitter.

Leave a Reply

Your email address will not be published. Required fields are marked *