0

Help! I tried all answers on SO but to no avail

Shape xml:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line">
    <!-- stroke width must be 1px, see https://stackoverflow.com/questions/3979218/android-listview-divider -->  
    <stroke 
        android:width="1px"
        android:color="#aaAAaa" />  
</shape>

In ListFragment:

public void onViewCreated(View v, Bundle bundle){
  listView = getListView();
  listView.setDivider(getActivity().getResources().getDrawable(R.drawable.xxxx));
  listView.setDividerHeight(1);
  setListAdapter(...);
}

Adapter:

@Override public boolean areAllItemsEnabled(){return true;}

Also, the divider should not span the entire listview's width, I want to have some padding on the left and right. So I think a shape drawable is a must.

EDITED Answer: Don't use line, use a rectangle instead. and follow this for padding: How to add padding to gradient <shape> in Android?

Community
  • 1
  • 1

3 Answers3

0

In xml of listview specify following :

 <ListView
        android:id="@+id/List"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1.0"
        android:divider="#35FFFFFF"
        android:dividerHeight="1dp" >
    </ListView>

The divider color and height can be anything that you want.

Akanksha Hegde
  • 1,738
  • 11
  • 14
  • You can use somethinh like this, lv = getListView(); lv.setDivider(getActivity().getResources().getDrawable(android.R.color.black)); lv.setDividerHeight(1); – Akanksha Hegde Apr 25 '14 at 12:08
  • Just bear in mind that setDividerHeight works in px, not in dp. Some conversion is needed if you intend to use dps instead. – Phantômaxx Apr 25 '14 at 12:16
0

Try using,

listView = getListView();
listView.setDivider(new ColorDrawable(Color.parseColor("#aaAAaa")))
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1,getResources().getDisplayMetrics());
listView.setDividerHeight((int)px)

P.S. updated code to get dip value for given int value. You can pass the value on place where I've set 1.

AndyN
  • 1,742
  • 1
  • 15
  • 30
  • Just bear in mind that setDividerHeight works in px, not in dp. Some conversion is needed if you intend to use dps instead. – Phantômaxx Apr 25 '14 at 12:16
0

Use

 <ListView
    android:id="@+id/listview"   
    android:dividerHeight="3dp" 
    android:divider="#05806D"
    android:listSelector="#ffffff"
    android:choiceMode="singleChoice"
    android:layout_width="fill_parent"
     android:layout_height="fill_parent" >
</ListView> 

let me know till you get the solution or not ?

Subhalaxmi
  • 5,687
  • 3
  • 26
  • 42