Buneme Kyakilika
Buneme Kyakilika

Reputation: 1202

Alternating Listview in Android xml

I woud like to create a listview that alternates background images. For example, the first item would have background image a and the second item would have background image b and the third backgroud a. In basic terms I would like help on creating a listview that for every odd item (egg first, third, fith) has a certian background image different to those listview items which are even (egg second, fourth, and sixth listview item). Here's an exampe.

http://www.gadgetreview.com/wp-content/uploads/2011/10/SIRI-Reminders.jpg

In this example the speech bubbles are background image and each different background image is a different listview item.

Upvotes: 1

Views: 445

Answers (4)

Tooroop
Tooroop

Reputation: 1884

In your list adapter in the getView method divide the position attribute that gets sent into the method by 2. If the remaining number is 0 than you are in the even row of your listview. Depending on that you can change the layout of your list view item.

@Override
public View getView(int position, View convertView, ViewGroup parent) {

if(position % 2 = 0)
//set layout for even row
}else{
//set layout for odd row
}

Upvotes: 2

BigBen3216
BigBen3216

Reputation: 867

you have to make your own custom adapter and then in the following method:

public View getView(int position, View convertView, ViewGroup parent) {

} 

you can use position to change the background if position is odd

Upvotes: 1

enrico.bacis
enrico.bacis

Reputation: 31524

Last time that I tried I didn't find an xml parameter to do that, but you can try to use the same workaround used in this question:

Stack Overflow: How do I alternate colors in between Listviews?

Upvotes: 2

Terril Thomas
Terril Thomas

Reputation: 1506

There is SetEmptyView method in list view use it

  listView.setEmptyView( findViewById( R.id.empty_list_view ) );

Upvotes: 0

Related Questions