views:

33

answers:

1

Hi,

I have four tabs that hold four listviews, I want to set a background for each list view but whenever I try to add the background it puts the image in each cell of the listview instead of behind the list.

?xml version="1.0" encoding="utf-8"?>

TextView xmlns:android="http://schemas.android.com/apk/res/android"

android:background="@drawable/pre"

android:layout_width="fill_parent" android:layout_height="fill_parent"

android:padding="10dp"

android:textSize="21sp">

/TextView>

I've realised that this is beacuse I've tried to add the background in a textview so it adds the image in each cell in the listview, so I have tried to add a linearlayout, listview and a imageview and put the background there but it force closes. I think this is becuse the tabhost uses main.xml to draw the main page and it conflicts, so I even tried to add the listview there still it nforce closes, it will only work if I have a textview only, howe can I add a background to each listview, below is the listview code;

public class prem extends ListActivity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    // Create an array of Strings, that will be put to our ListActivity
    String[] names = new String[] { "Pre"};
    ListView lv = getListView();    
    lv.setCacheColorHint(00000000);    
    lv.setAdapter(new ArrayAdapter<String>(this,
            R.layout.list_item, names));

}
+2  A: 

Okay, your XML layout files are going to be used in the setContentView() method. You haven't posted the code for your activity which includes the TabHost, but I'll assume you're using the default setContentView(R.layout.main);. If you're NOT using setContentView() (in the case of your ListActivity), adding a ListView to an XML file isn't going to change anything, because it's never being used.

You are correct in that you're having the issue because you're setting the background of the TextView. Since you're using a ListActivity, you'll need to set the ListView's background using code. ListView is a subclass of View, so you can use the methods from the View class to set your background resource for the ListView.

For example:

ListView listView = getListView();

//set background to color
listView.setBackgroundColor(#FF888888);

//set background to Drawable
listView.setBackgroundDrawable(myDrawable);

//set background to Resource
listView.setBackgroundResouce(R.id.my_res_id);
kcoppock