Top and bottom dividers not showing in Android listview
Asked Answered
A

5

19

The way I understand it, the divider defined for a listview should appear at the top and bottom of the list too, i.e. above the first item and under the last item.

For some reason they don't appear in my listview:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="@drawable/background">

<ImageView
    android:id="@+id/home"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="3dp"
    android:paddingTop="3dp"
    android:paddingBottom="3dp"
    android:src="@drawable/homeicon"/>

<TextView
    android:id="@+id/titleBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Page Title" 
    android:layout_marginLeft="10dp" 
    android:paddingTop="3dp"
    android:textSize="18sp" 
    android:textStyle="bold" 
    android:typeface="serif" 
    android:textColor="#FFFFFF"
    android:layout_centerHorizontal="true"/>

<ImageView
    android:id="@+id/back"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingRight="8dp"
    android:paddingTop="8dp"
    android:src="@drawable/backicon" 
    android:layout_alignParentRight="true"/>


<ImageView
    android:id="@+id/separator1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/home"
    android:scaleType="fitXY"
    android:src="@drawable/separatorimg" />


<ListView
    android:id="@android:id/android:list" 
    android:layout_below="@+id/separator1"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:cacheColorHint="#00000000"
    android:divider="@drawable/separatorimg">
</ListView>

</RelativeLayout>

Setting android:footerDividersEnabled to true (which is the answer given in other questions around that matter) doesn't change anything. Also I have no disabled items, unless there is somehow a default footer that would be disabled.

Any ideas?

Edit: It seems the divider at the top and bottom dividers appear for a split second when I scroll past the first/last item, and then disappears. It looks as if interface was going to continue, only later to realize that was the last item and then disable the divider.

Ansilma answered 27/8, 2012 at 9:27 Comment(0)
I
30

I solved this problem using help from the following link: Divider disappears from last item in listview if footerview added

What I did was add the

  android:footerDividersEnabled="true" 

attribute to the ListView in XML. Then programatically, I added an empty footer view that was selectable using

 listView.addFooterView(new View(context), null, true);

I didn't try the header, but I expect it to work the same way.

Illusory answered 26/7, 2013 at 13:23 Comment(2)
For me, the divider only appears if the footer is marked as selectable i.e. that final parameter to addFooterView is true. Setting it to false doesn't show the divider.Curio
Attention:By using this code (invoicListview.addHeaderView(new View(this), null,true )) by clicking on the item, a number more than the item number is returned.Dixie
A
4

I had the same problem. Here's how I solved it:

<ListView
    android:id="@+id/my_list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:divider="@color/transparent"
    android:dividerHeight="@dimen/padding_default"
    android:footerDividersEnabled="false"
    android:headerDividersEnabled="false" >
</ListView>

Then, in the onCreate() method, I have:

View v = new View(this);
v.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 1));
mList.addFooterView(v);
Auricle answered 25/4, 2013 at 3:25 Comment(1)
It collapsed all items in the listPurgatory
B
0

I think you have to set footer using a view below the listview. i had the same problem and this is what i could get as a solution:

Show divider after the last item in the list

Baumgartner answered 27/8, 2012 at 9:37 Comment(4)
Thanks for the answer, unfortunately that doesn't work for me. First, the view only shows if there is another element below it (don't know why), and second even when it shows, it stays always visible at the bottom of the screen. The effect I'm trying to reach is that the divider only appears at the end of the list, as if it scrolls up with it.Ansilma
I tried it, copied the code in the accepted answer (replacing the listview id) just below the listview. I then get a green line at the bottom of the screen even when there are many list items still to scroll through.Ansilma
okay mine was some custom layout with which it works like you wanted. But i just copied your code and tried, it works the way you wanted. only thing is the top and bottom divider wont show up unless you try to scroll down even after the last itemBaumgartner
Do you mean my code works as is (except for the showing up only when trying to scroll past) or the solution to your question works with my code? Because I don't even see the green line unless I define a height (for example 300dp) instead of wrap_content.Ansilma
E
0

Please find the modified code below :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background" >

<LinearLayout
    android:id="@+id/llAppBar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:orientation="horizontal"
    android:weightSum="10" >

    <ImageView
        android:id="@+id/home"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:paddingBottom="3dp"
        android:paddingLeft="3dp"
        android:paddingTop="3dp"
        android:scaleType="fitXY"
        android:src="@drawable/homeicon" />

    <TextView
        android:id="@+id/titleBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_weight="6"
        android:paddingTop="3dp"
        android:text="Page Title"
        android:textColor="#FFFFFF"
        android:textSize="18sp"
        android:textStyle="bold"
        android:typeface="serif" />

    <ImageView
        android:id="@+id/back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:paddingRight="8dp"
        android:paddingTop="8dp"
        android:scaleType="fitXY"
        android:src="@drawable/backicon" />
</LinearLayout>

<ImageView
    android:id="@+id/separator1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/llAppBar"
    android:scaleType="fitXY"
    android:src="@drawable/separatorimg" />

<ListView
    android:id="@android:id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/separator1"
    android:cacheColorHint="#00000000"
    android:divider="@drawable/separatorimg" >
</ListView>

</RelativeLayout>
Epigraphy answered 27/8, 2012 at 9:40 Comment(1)
I guess you added the LinearLayout to be able to use weight. Good idea, but no luck, still no divider appearing top or bottom of the list.Ansilma
E
-2

I had the same problem, and setting padding and background color solved it.

<ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/gray_background"
    android:paddingTop="1dp"
    android:paddingBottom="1dp"        
/> 

Here, background color should be same with that of divider with the listview.

Eleventh answered 12/6, 2015 at 3:40 Comment(1)
This solution doesn't disappear when you scrollOstosis

© 2022 - 2024 — McMap. All rights reserved.