Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign padding to Listview item divider line

enter image description here

How can i give padding to list item as i show in image. I want to make the divider in the layout as shown in the image.

this is my list fragment code

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" 
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp">

 <ListView 
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
     android:id="@+id/listV_main"
    android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"/>

This is my List section code

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:paddingRight="10dp"
android:paddingBottom="10dp">
<include
    android:id="@+id/list_item_section_text"
    layout="@android:layout/preference_category"
     />

This is my list item code

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:adjustViewBounds="true"
android:paddingRight="?android:attr/scrollbarSize"
   >
<ImageView
    android:id="@+id/showlist_item_entry_drawable"
    android:layout_width="82dp"
    android:adjustViewBounds="true"
    android:layout_height="68dp"
    android:scaleType="fitXY"
    android:paddingLeft="9dp"/> 
<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dip"
    android:layout_marginRight="6dip"
    android:layout_marginTop="6dip"
    android:layout_marginBottom="6dip"
    android:layout_weight="1"
>
    <TextView android:id="@+id/list_item_entry_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:ellipsize="marquee"
        android:fadingEdge="horizontal" /> 
    <TextView android:id="@+id/list_item_entry_summary"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/list_item_entry_title"
        android:layout_alignLeft="@id/list_item_entry_title"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:singleLine="true"
        android:textColor="?android:attr/textColorSecondary" />
</RelativeLayout>

like image 417
Swap-IOS-Android Avatar asked Dec 27 '12 12:12

Swap-IOS-Android


2 Answers

Use 'inset'.....

(list_divider.xml)

<?xml version="1.0" encoding="UTF-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetLeft="50dp"
    android:insetRight="50dp" >

 <shape>
    <solid android:color="@color/orange" />
    <corners android:radius="2.0dip" />
</shape>

</inset>

and in your list view add like this...

<ListView
    android:dividerHeight="2dp"
    android:divider="@drawable/list_divider"
    ...
/>

you can set the inset value as desired...

UPDATE

As pointed out by @Giulio Piancastelli , If the background of list container is different from background of list item then you may use 'layer-list'...

(list_divider.xml)

  <?xml version="1.0" encoding="utf-8"?>
  <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >  
   <item>
        <shape android:shape="rectangle" >
            <solid android:color="@color/list_background" />
        </shape>
    </item>


    <item
        android:left="10dp"
        android:right="10dp">
        <shape android:shape="rectangle" >
            <solid android:color="@color/divider_color"/>
        </shape>
    </item>

        </layer-list>

and in your list view add like this...

<ListView
    android:dividerHeight="2dp"
    android:divider="@drawable/list_divider"
    ...
/>
like image 137
ASP Avatar answered Oct 14 '22 03:10

ASP


You need padding for dividers? Create your custom drawable-shape as:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:left="10dp"
        android:right="10dp">
        <shape android:shape="rectangle" >
            <solid android:color="@android:color/black" />
        </shape>
    </item>

</layer-list>

And set as divider for your ListView in xml:

<ListView
    android:dividerHeight="2dp"
    android:divider="@drawable/custom_divider"
    ...
/>

UPD

I just have ListView in xml:

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dividerHeight="2dp"
android:divider="@drawable/line"
/>

Divider as line.xml in drawable:

<?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
        <item
        android:left="10dp"
        android:right="10dp">
        <shape android:shape="rectangle" >
            <solid android:color="@android:color/black" />
        </shape>
    </item>
</layer-list>

Do not make any modifications on the ListView in code. You can try to use Clean if resources are wrong builded.

like image 19
nfirex Avatar answered Oct 14 '22 03:10

nfirex