Question

I'm new to android development. I have the following code to create listview:

public class SuggestActivity extends ListActivity {

    private List<Map<String, Object>> mData;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mData = getData();
        MyAdapter adapter = new MyAdapter(this);
        setListAdapter(adapter);
    }

Now i want to set background image for the whole screen. notice: not the item in listview. How should i do?

Was it helpful?

Solution

You need to retrieve the ListView:

ListView list = getListView();

Then, set a color by using setBackgroundColor method as follows:

// use a color in colors.xml file  
list.setBackgroundColor(getResources().getColor(R.color.white)); 

Same with setBackgroundDrawable with a drawable resource:

list.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_list));

OTHER TIPS

Use the setBackgroundDrawable Method:

getListView().setBackgroundDrawable(drawable);

Set background property to listview.

<ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/bg" >
    </ListView>

Here, the background of the listview will be assigned an image that you should place in your 'res/drawable' folder. The extension for the image is omitted, so the image bg.png appears as 'bg'

notice that you used the ListActivity, according to d.android.com:

ListActivity has a default layout that consists of a single, full-screen list in the center of the screen. However, if you desire, you can customize the screen layout by setting your own view layout with setContentView() in onCreate(). To do this, your own view MUST contain a ListView object with the id "@android:id/list" (or list if it's in code)

which means if you setListAdapter, the ListActivity will call setContentView(com.android.internal.R.layout.list_content_simple);

Set background in XML

<ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/bg" >
</ListView>

Java code

ListView ls;
    ls.setBackgroundColor(Color.RED);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top