1

I need to show some customized divider using xml layout. So is there any possible to do like this

<ListView
                    android:id="@+id/volunteer_list"
                    android:layout_width="match_parent"
                    android:divider="@layout/separator"
                    android:dividerHeight="5dp"
                    android:layout_height="wrap_content"/>

?

Please check this attached screenshot. there you can see the custom divider. like one i need to set in android. your comments are much welcome. enter image description here

rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
itmani1990
  • 13
  • 7

3 Answers3

1

If you are using ListView, you can certainly follow Daryl's answer. However, the divider will be there even after the last item. I will certainly suggest switching to RecyclerView which is an advance version of ListView with more power and features. In RecyclerView, do it like this:

Create a line diveder drawable. (Change the size and color according to your need).

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

<size
    android:width="4dp"
    android:height="4dp"/>

<solid android:color="#d5d5d5" />

Create a DividerItemDecorator class.

public class DividerItemDecorator extends RecyclerView.ItemDecoration {
private Drawable mDivider;

public DividerItemDecorator(Context context) {
    mDivider = ContextCompat.getDrawable(context, R.drawable.line_divider);
}

@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
    int left = parent.getPaddingLeft();
    int right = parent.getWidth() - parent.getPaddingRight();

    int childCount = parent.getChildCount();
    for (int i = 0; i < childCount; i++) {
        View child = parent.getChildAt(i);

        RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();

        int top = child.getBottom() + params.bottomMargin;
        int bottom = top + mDivider.getIntrinsicHeight();

        mDivider.setBounds(left, top, right, bottom);
        mDivider.draw(c);
    }
}

}

Finally, set the DividerItemDecorator in your Recycler View using this line of code :

recyclerView.addItemDecoration(new DividerItemDecorator(getApplicationContext()));
Basu
  • 763
  • 8
  • 27
0

Try the following, i had done like this to achieve : main_listview.xml

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/listviewLayout"
    android:visibility="visible">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/listview"
        android:visibility="gone">
    </ListView>

</LinearLayout>

list_row.xml

<LinearLayout
            android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginLeft="10dip"
        android:orientation="vertical" >

        <com.altss.asterridepassenger.utils.customview.CustomTextView
            android:id="@+id/search_list_value"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="@android:color/black"
            android:layout_marginRight="15dip"
            android:textSize="14sp"
            aster:customFont="@string/font_lato_medium" />

        <View
            android:layout_width="fill_parent"
            android:layout_height="10dip"    //(increase as your wish) 
            android:layout_marginTop="10dip"
            android:background="@color/search_list_line_color" />
    </LinearLayout>

then code in activity as follows:

 adapter = new ArrayAdapter(this, R.layout.list_row.xml,        R.id.search_list_value,list); //list is your list items
vimal raj
  • 295
  • 1
  • 13
  • Thanks for the code. If so, the final list item also have this view right? is there any possible to avoid adding view at the final item of the listview. – itmani1990 Mar 23 '17 at 08:04
  • You can use Basu Singh's answer and modify the for loop counter to suit your needs, or you can use a ListView and in getView() do something like this: yourDividerView.setVisibility(position == adapterItem.size() - 1 ? Visibility.GONE : Visibility.VISIBLE) – aneurinc Mar 23 '17 at 08:33
  • @User123 Thanks. Ya its really helpful for me. – itmani1990 Mar 23 '17 at 10:23
  • No prob. Glad I could help. – aneurinc Mar 23 '17 at 11:06
0

Is any possible add xml layout in android listview divider?

NO its is not possible.

Now as you are looking for setting divider like Screenshot you attached, you can use drawable file for that.

make one drawable file say divider.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:gravity="center">
        <shape android:shape="rectangle">
            <solid android:color="#c3c3c3" />
        </shape>
    </item>
    <item
        android:height="1dp"
        android:gravity="top">
        <shape android:shape="rectangle">
            <solid android:color="#000" />
        </shape>
    </item>
    <item
        android:height="1dp"
        android:gravity="bottom">
        <shape android:shape="rectangle">
            <solid android:color="#000" />
        </shape>
    </item>
</layer-list>

now pass this file to you ListView as divider

<ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="@drawable/divider"
    android:dividerHeight="10dp" /> <!-- adjust according to your need -->

and you are good to go. it will give you exactly what you need.

Output

enter image description here

Happy Coding.

V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50