Search code examples
androidlistviewcustom-lists

set drawable listview background per item


I have the default list view as with the black background, I would like to have it like this:

enter image description here

or:

enter image description here

How do I achieve this?


Solution

  • If you using array adapter you should create two layouts. In one of them to define view of listview element, another must contain <listview> tag.

    In onCreate:

    public class ListAction extends Activity {
        private ListView lv1;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.list);
    
            lv1 = (ListView) findViewById(R.id.listView);
    
                ArrayAdapter<CharSequence> adapter = ArrayAdapter
                        .createFromResource(this, some array,
                                R.layout.list_items);
                lv1.setAdapter(adapter);
                cats = getResources().getStringArray(some array);
    
            lv1.setTextFilterEnabled(true);
            lv1.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> a, View v, int position,
                        long id) {
    
                }
            });
        }
    }
    

    You shold use R.array.<name> instead of some array

    Example of list_item.xml:

        <?xml version="1.0" encoding="utf-8"?>
        <TextView xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@android:id/text1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:paddingBottom="8.0dip"
            android:paddingLeft="12.0dip"
            android:paddingRight="3.0dip"
            android:paddingTop="8.0dip"
            android:textColor="#ffffffff"
            android:textSize="10.0pt"
            android:textStyle="italic"
            android:background="@drawable/list_bg"
            android:text="List Item" />
    

    Example of list.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"
        android:background="@color/white" >
    
        <ListView
            android:id="@+id/listView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:divider="@drawable/divider" />
    
    </LinearLayout>
    

    For more specific layout you can look at this link http://www.ezzylearning.com/tutorial.aspx?tid=1763429&q=customizing-android-listview-items-with-custom-arrayadapter


    Added: At first you can change background for entire layout in list.xml, also you can use custom divider (You should use tag divider as above).

    You can't add another layouts into list_item.xml, but you can use android:background to set item background. I never used that but I know it works. For more changes you should use simplearray or write your own adapter.

    Example how to use gradient (I'm using it for buttons):

    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle" >
        <corners android:radius="12px" />
        <gradient
            android:angle="90"
            android:endColor="#color2"
            android:startColor="#color1"
            android:type="linear" />
    </shape>