Search code examples
javaandroidlistviewcustom-lists

Setting background while using Custom ListView


I am working on a android application project in which I am using a customer simple adapter to repeat a layout called row.xml. Everything is going good except for the fact that I have a background color/image that I am using for my app and when I use this simple adapter method I get white space for the area that is not filled by the list.

Here is the code from my Java class

public class CustomerActivity extends ListActivity {

    final static ArrayList<HashMap<String, ?>> arrayListData = new ArrayList<HashMap<String, ?>>();

    ListView mCustomerList;
    static {
        HashMap<String, Object> CurrentRow = new HashMap<String, Object>();
        CurrentRow.put("Icon", R.drawable.has_360_inspection_icon);
        CurrentRow.put("Icon2", R.drawable.has_solid_fuel_inspection_icon);
        CurrentRow.put("Name", "Xue Test");
        arrayListData.add(CurrentRow);
        CurrentRow = new HashMap<String, Object>();
        CurrentRow.put("Icon", R.drawable.has_360_inspection_icon);
        CurrentRow.put("Icon2", R.drawable.no_solid_fuel_inspection_icon);
        CurrentRow.put("Name", "Ryan Test");
        arrayListData.add(CurrentRow);
        CurrentRow = new HashMap<String, Object>();
        CurrentRow.put("Icon", R.drawable.no_360_inspection_icon);
        CurrentRow.put("Icon2", R.drawable.has_solid_fuel_inspection_icon);
        CurrentRow.put("Name", "Becca Test");
        arrayListData.add(CurrentRow);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SimpleAdapter adapter = new SimpleAdapter(this, arrayListData, R.layout.row, new String[] { "Icon", "Icon2", "Name" },
                new int[] { R.id.imageView1, R.id.imageView2, R.id.customer_name_textView });
        setListAdapter(adapter);

    }

and here is the code from row.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/lightBlue"
    android:weightSum="6" >

    <TextView
        android:id="@+id/customer_name_textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="TextView" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/has_360_inspection_icon" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/has_solid_fuel_inspection_icon" />

    <View
        android:id="@+id/view1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="2" />

</LinearLayout>

Thank you for the help and if you need any more information just let me know.


Solution

  • If you just want the entire list to have a light blue background, set it on the listview itself instead of on each row. That way if there's empty space in the list it will still be blue.