Home » Android » android – Divider disappears from last item in listview if footerview added

android – Divider disappears from last item in listview if footerview added

Posted by: admin May 14, 2020 Leave a comment

Questions:

If footer view added in ListView, then divider disappears from last item of ListView.

Even I have set android:footerDividersEnabled="true" with ListView and my footer view is just TextTiew.

How to&Answers:

The ListView implementation in Android never draws dividers between items that are disabled, which if you are just calling the addFooterView(View v) method then by default your footer will be.

Instead you need to call the addFooterView(View v, Object data, boolean isSelectable) method with isSelectable set to true. You can just pass null for the data object if you don’t need it.

Answer:

Setting isSelectable to true didn’t work for me, maybe because I was also calling removeFooterView when my list was done loading.

What finally fixed it for me was setting android:layout_height to “fill_parent” instead of “wrap_content” on the ListView.

Answer:

This almost worked for me. I was after a divider after the last list item, but not after the footer as my footer was empty space. I ended up adding two footers, one selectable of zero height and one not selectable containing the padding.

TextView view = new TextView(this);
view.setLines(0);
TextView view1 = new TextView(this);
view1.setLines(4);
mListView.addFooterView(view, null, true);
mListView.addFooterView(view1, null, false);
mListView.setFooterDividersEnabled(true);

Answer:

Try setting the layout_height of the ListView to match_parent:

android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#333333"
android:dividerHeight="1px"

When the layout_height is set to wrap_content it might skip the bottom divider:

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#333333"
android:dividerHeight="1px"

Answer:

Head through the wall approach, but reliable, is to manually add divider as a footer view.

ListView myListView = (ListView) view.findViewById(R.id.my_list_view);
myListView.addFooterView(getInflater().inflate(R.layout.horizontal_divider, myListView, false), null, false);
myListView.addFooterView(getInflater().inflate(R.layout.the_original_footer_view, myListView, false), null, false);

Where the layout file would look like this:

<View xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="1px"
    android:background="?android:attr/dividerVertical" />

This approach can be used to add the divider easily even after the last footer, regardless it is selectable, enabled or anything – it just stays there.

Note the height is 1px rather than 1dp. Though against all recommendations, at least at the device I tested this gives the same divider height as ListView, while 1dp does not.