Kalimah
Kalimah

Reputation: 11447

Adding background image to listview in Android?

I am trying to add a background image to a listActivity in my app. However, with the code that I am using the background image displays on every single row rather than for the whole list.

This is the xml file:

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

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:src="@drawable/qbg1"/>

<TextView
    android:id="@+id/employeeID"
    android:layout_width="50px"
    android:layout_marginRight="20px"
    android:layout_height="wrap_content"
    android:textColor="@color/textColor"/>

<TextView
    android:id="@+id/employeeName"
    android:layout_width="wrap_content"
    android:layout_marginRight="10px"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/employeeID"
    android:textColor="@color/textColor"/>


</RelativeLayout>
<ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:src="@drawable/qbg1"/>

what is the best approach to add a background image to the activity? I would prefer to do in the XML file rather than programmatically.

Upvotes: 3

Views: 13458

Answers (3)

Kalimah
Kalimah

Reputation: 11447

I solved the issue by adding the image background to to the main view and then add the listView inside that view.

So this is my main.xml file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="4px"
android:background="@drawable/qbg"> 

<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:drawSelectorOnTop="false"
             android:cacheColorHint="#00000000"/> //This is to fix disappearing background issue
</RelativeLayout>

and this is my listView.xml

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

<TextView
android:id="@+id/employeeID"
android:layout_width="50px"
android:layout_marginRight="20px"
android:layout_height="wrap_content"
android:textColor="@color/textColor"/>

<TextView
android:id="@+id/employeeName"
android:layout_width="wrap_content"
android:layout_marginRight="10px"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/employeeID"
android:textColor="@color/textColor"/>

</RelativeLayout>

Hope this will beneficial for other people.

Upvotes: 9

Amalan Dhananjayan
Amalan Dhananjayan

Reputation: 2287

Did you try to set the image in the code, try this getListView().setBackgroundResource(R.drawable.matchback);

Upvotes: 2

FoamyGuy
FoamyGuy

Reputation: 46856

I have used something like this to get that effect.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/paper"
>
<ExpandableListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="wrap_content" 
android:fadingEdge="vertical"
android:fadingEdgeLength="10dip"
android:scrollingCache="false"


>
</ExpandableListView>
</RelativeLayout>

I remember having to tweak some of the settings to make it so that the background stayed visible while the list was being scrolled however.

Upvotes: 2

Related Questions