I am using Android Listview and and I want a 1px divider on both the side of the listview item means on the top and bottom of two different colours. But the problem is that I am not getting login for showing divider in bottom. I tried android:layout_below
but it is shown as invalid.
This is the Listview code
<ListView
android:id="@+id/myphnview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@drawable/dividerheight"
android:background="#E9EAEC"
android:clickable="true"
android:divider="@drawable/dividerheight" >
</ListView>
This is the xml file I am using for top border. dividerheight.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="line" >
<stroke android:color="#c6c7c9" />
<size android:height="1px" />
</shape>
</item>
</layer-list>
This is the layout of my row
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rowlayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#E9EAEC"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="2" >
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@drawable/ic_launcher"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="1" >
<TextView
android:id="@+id/file_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_margin="5dp"
android:text="Hello Android "
android:textColor="@android:color/black"
android:textSize="20dp" >
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp" >
<ImageView
android:id="@+id/share_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:background="@drawable/ic_launcher"/>
</LinearLayout>
</LinearLayout>
separated creates a fixed-length, scrollable , linear array of list “items” separated by list item “separators”. The itemBuilder callback is called whenever there are indices ≥ 0 and< itemCount . The separatorBuilder callback will be called with indices greater than or equal to zero and less than itemCount - 1 .
Android ListView is a ViewGroup that is used to display the list of items in multiple rows and contains an adapter that automatically inserts the items into the list. The main purpose of the adapter is to fetch data from an array or database and insert each item that placed into the list for the desired result.
A list view is an adapter view that does not know the details, such as type and contents, of the views it contains. Instead list view requests views on demand from a ListAdapter as needed, such as to display new views as the user scrolls up or down. In order to display items in the list, call setAdapter(android.
Creating Widget Build area -> Material App -> Scaffold Widget -> ListView. builder() widget. After ListTile widget we would call the Divider() widget. The Divider widget would automatically draw a horizontal line between each list item.
There are several ways to achieve this. One simple way would be to hide dividers completely (set divider width to 0 or divider to null) in the list and just have each list item to contain a line at the top and a line at the bottom. Consider this xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentTop="true"
android:background="@android:color/white" />
<LinearLayout ...>
<!-- this is your current list item LinearLayout -->
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:background="@android:color/black" />
</RelativeLayout>
As you are already using a custom adapter, it's just a matter of using an XML like above for your list item layout in the getView
method of the Adapter. This will produce list items 60dp high with a white line along the top and a black line along the bottom.
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