0

I have following layout for a list view item.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/border"
    android:orientation="vertical"
    android:paddingBottom="1dp"
    android:paddingLeft="0dp"
    android:paddingRight="0dp"
    android:paddingTop="0dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/background_light">

        ....

    </LinearLayout>

</LinearLayout>

The background of outer layout (@drawable/border) is grey color, and since the paddingBottom of outer layout is 1dp and inner layout background is white, there is a grey line between each list view item.

But, in low res (ldpi) devices, this grey line is not visible.

If I change the padding of outer layout to 2dp, grey line becomes visible, but it looks too thick in high res devices. A better result can be achieved by using 2px instead of 2dp.

What is the best way to show the grey border line in low res devices?

Lahiru Chandima
  • 22,324
  • 22
  • 103
  • 179

1 Answers1

1

You can set the divider and the divider height by adding them as parameters to your list view:

<ListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:divider="@drawable/divider"
    android:dividerHeight="2px">
</ListView>

Also it's highly recommended to use RecyclerView instead ListView, take a look on the accepted answer. Android Recyclerview vs ListView with Viewholder

Setting divider on RecyclerView (Android Support Library 25.0.0 and above):

DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(context, DividerItemDecoration.VERTICAL);
recyclerView.addItemDecoration(dividerItemDecoration);
Alex Kamenkov
  • 891
  • 1
  • 6
  • 16