Search code examples
androidbackgroundhorizontallist

Horizontal list view background


I got a problem regarding a small application where i am using Custom horizontal list view i have followed the below link for creating the horizontal list view.

And xml layout is like this..

 <com.example.HorizontalListView
      android:id="@+id/listview"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal"
      android:background="@android:color/transparent"
      android:cacheColorHint="@android:color/transparent"
      android:divider="@color/Black"
       >
  </com.example.HorizontalListView>

But the problem is, i cannot change the background for the horizontal list view,could anybody help me ..@thanks in advance!!!


Solution

  • Try changing the background dynamically.

    Please click here for APIs for View from Android Developer Reference page. Keep in mind that below three methods can be used to add backgrounds to the View object:

    public void setBackground (Drawable background);
    public void setBackgroundColor (int color);
    public void setBackgroundResource (int resid);
    

    onCreate:

    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
    
        setContentView(R.layout.listviewdemo);  
    
        HorizontialListView listview = (HorizontialListView) findViewById(R.id.listview);  
        listview.setAdapter(mAdapter);  
    
    }  
    

    BaseAdapter:

    private BaseAdapter mAdapter = new BaseAdapter() {  
    
        @Override  
        public View getView(int position, View convertView, ViewGroup parent) {  
            View retval = LayoutInflater.from(parent.getContext()).inflate(R.layout.viewitem, null);  
            retval.setBackgroundResource(R.id.my_background); // add this line
    
            return retval;  
        }  
    
    };