Custom listdivider style

I am trying to create a default ListView list separator in order to have spaces on both sides. I already tried to set the fields of the entire ListView, but did not create the desired result.

What I have:

What i have

What I'm trying to do (note the fields on both sides of the delimiters):

what i need to have

What would be the best way to do this? Thanks in advance!

+5
source share
3 answers

Ultimately, I was able to solve my problem using the accepted answer of this (without the gradient part)

+6
source

use <include layout="@layout/divider"/>and custom layout for the string. 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();       
            }
      });
    }
}
+10

I would suggest creating your own ListAdapterand installing it in yours ListView. Then, in your method, getViewyou can individually configure individual list items.

0
source

All Articles