Mohammad Umair Khan
Mohammad Umair Khan

Reputation: 515

Setting Background Image for a ListView

Hey Guys facing a problem with setting a background image on my ListView. Code is as below

history.xml

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

<ListView android:id="@id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:cacheColorHint="#00000000"
    android:layout_weight="1"
    android:drawSelectorOnTop="false"
    android:src="@drawable/back">
    </ListView>

<TextView  android:id="@id/android:empty"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#FFff00"
    android:text="No data"
    android:cacheColorHint="#00000000"
    />
</LinearLayout>

rowlayout.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">

<TextView android:id="@+id/ProductName"       
    android:textSize="25sp"         
    android:textColor="#FFFF00"        
    android:layout_width="fill_parent"         
    android:layout_height="wrap_content"
    />

</LinearLayout>

Java Code

...

setListAdapter(new ArrayAdapter<String>(this, R.layout.rowlayout,R.id.ProductName,Names));

...

I dont know what i am doing here but the background does not appear. Its black throughout. If i put the background on the text view in rowlayout.xml then it appears but its like whole screen carries one entry of the ListView. Tried alot of tutorials. Maybe doing some rookie mistake.. help me out please

Upvotes: 2

Views: 5084

Answers (4)

joates
joates

Reputation: 1103

I believe what you're experiencing is described here: http://android-developers.blogspot.com/2009/01/why-is-my-list-black-android.html

Because of an optimization called "cache color hint", the background of your list is being set by default to the window's background color, #191919 in Android's dark theme.

If you set a background on each row, I think you'll see what I mean. Try setting the following background shape:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<stroke
    android:color="@color/mid_gray"
    android:width="1dp"
/>

<padding
    android:left="7dp"
    android:top="7dp"
    android:right="7dp"
    android:bottom="3dp"
/>

<corners android:radius="10dp" />

<solid android:color="@android:color/white" />
</shape>

like this:

<?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="@drawable/shape_data_background">

<TextView android:id="@+id/ProductName"       
android:textSize="25sp"         
android:textColor="#FFFF00"        
android:layout_width="fill_parent"         
android:layout_height="wrap_content"
/>

</LinearLayout>

Upvotes: 0

teoREtik
teoREtik

Reputation: 7916

As I understand your code concept correctly you want to achieve two Views lying on the LinearLayout, one of which (TextView in your case) cover layout with it's background and another one is transparent. So there is no other way than to set background for your layout and make your ListView transparent, using android:background="#00000000" and also you should set android:cacheColorHint="#00000000" to avoid filling your ListView background with black during scrolling

Upvotes: 0

mr.j05hua
mr.j05hua

Reputation: 170

use android:background="#00000000" on your listview items, then set the parent forms background to your image

Upvotes: 0

Pinki
Pinki

Reputation: 21929

use android:background="@drawable/icon" for listview like

 <ListView android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000"
android:layout_weight="1"
android:drawSelectorOnTop="false"
android:background="@drawable/icon">
</ListView>

Upvotes: 2

Related Questions