Question

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!!!

Was it helpful?

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;  
    }  

};  

OTHER TIPS

I have a solution. I'm also playing around with HorizontalListView. First you need to add this line within the <LinearLayout> tag of viewitem.xml:

android:background="@android:color/transparent"

Then do this in listviewdemo.xml:

<com.devsmart.android.ui.HorizontalListView
    android:id="@+id/listview"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/background_you_want"
/>

If you did this correctly you'll see the background showing through the HorizontalListView. The background stretches across the entire screen and it makes sense because the HorizontalListView also spans the entire screen as well.

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top