5

I'm trying to style the default ListView list divider to have margins of whitespace on both sides. I already tried to set the margins of the entire ListView but that didn't create the desired result.

What i have:

What i have

What i'm trying to make (notice the margins on both sides of the dividers):

what i need to have

What would be the best way of doing this? Thanks in advance!

Bart
  • 769
  • 2
  • 13
  • 29

2 Answers2

11

use <include layout="@layout/divider" /> and custom layout for line. like this:

main.xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

<ListView android:id="@+id/listview"
    android:layout_width="wrap_content"
    android:divider="@layout/divider"
    android:dividerHeight="3sp"
    android:layout_height="wrap_content" />
</LinearLayout>

divider.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <gradient 
        android:startColor="#00000000"
        android:centerColor="#FFFFFF"
        android:endColor="#00000000" />
    <corners 
        android:radius="4dp" />
</shape>

AndroidlistviewdividerActivity.class

public class AndroidlistviewdividerActivity extends Activity {
    /** Called when the activity is first created. */
    private ListView lv;
    private String listview_array[] = { "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX",
                                    "SEVEN", "EIGHT", "NINE", "TEN" };
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        lv = (ListView) findViewById(R.id.listview);
        lv.setAdapter(new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, listview_array));
        lv.setTextFilterEnabled(true);
        lv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
                // TODO Auto-generated method stub
                 AlertDialog.Builder adb = new AlertDialog.Builder(
                        AndroidlistviewdividerActivity.this);
                 adb.setTitle("ListView OnClick");
                 adb.setMessage("Selected Item is = "
                    + lv.getItemAtPosition(arg2));
                 adb.setPositiveButton("Ok", null);
                 adb.show();       
            }
      });
    }
}
Nipun
  • 990
  • 1
  • 16
  • 25
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • Thank you, this is pretty much what i'm looking for. But if i set the ListView divider to @layout/divider the app crashes. Any suggestions? I get the following error: Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #2: invalid drawable tag View – Bart Mar 08 '12 at 17:04
  • @Meatje: have you get solution or not? – ρяσѕρєя K Mar 08 '12 at 17:30
  • @Meatje: ok fine, i'm updating my solution which is perfectly working my side.. – ρяσѕρєя K Mar 08 '12 at 17:32
  • Thanks for your replies, but no this is not the solution i am looking for. The code works but i was nog looking for a gradient. I need the default android divider style. – Bart Mar 08 '12 at 18:06
0

I would suggest creating your own ListAdapter and setting it into your ListView. Then in its getView method you can style individual list items in any way you want.

Aleks G
  • 56,435
  • 29
  • 168
  • 265