Use This:
getListView().setBackground(getResources().getDrawable(R.drawable.background));
Question
This is my android activity which uses a listactivity. I need to give a background image which is located at the res->drawable-hdpi folder. This is Bid_History.java page. In the corresponding activity xml page I have not given any list view. The program works fine without any problem. But I need to set a background to this listactivity .The background image to be used is located in the res->drawable-hdpi folder. I have tried many ways, but still it won't work at all. Can someone please help me in setting a background image for this activity.
package com.example.onlineauction;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.database.DataSetObserver;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class Bid_History extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_bid__history);
setListAdapter(new ArrayAdapter<String>(Bid_History.this, android.R.layout.simple_list_item_1,arraydetails));
//arraydetails is an arraylist which contains the data to be displayed in the listactivity.
//Data part here.
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
// Data part here
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.bid__history, menu);
return true;
}
}
This is my xml page.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Bid_History" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
Solution
Use This:
getListView().setBackground(getResources().getDrawable(R.drawable.background));
OTHER TIPS
Set the background image in the code as getListView().setBackgroundResource(R.drawable.bckimage);
Also add getListView().setCacheColorHint(android.R.color.transparent);
You can define a layout for your list activity. Make sure it has a ListView with id android:list. Add a background drawable to the layout. Use this layout in OnCreate (setContentView) method.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:alpha="0.6"
android:background="@drawable/background_img" >
<ListView android:id="@+id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>