Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom style listdivider [duplicate]

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!

like image 216
Bart Avatar asked Mar 08 '12 15:03

Bart


1 Answers

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();       
            }
      });
    }
}
like image 107
ρяσѕρєя K Avatar answered Sep 27 '22 23:09

ρяσѕρєя K