Tutorial details

Android listview background row style: Rounded Corner, alternate color | App Code for Sale | Preview

Android listview background row style: Rounded Corner, alternate color | Android Tutorial

How to create rounded corners and alternate the background color

Overview PAGE TOP

One aspect we didn’t consider in the previous posts is how we can apply style or background to the Listview items (or row). We can customize the look of the ListView in the way we like, for example with can use as background rounded corner, alternate color and so on. By now we considered just custom adapter without taking into account how we can customize how each item inside the listview appears.

In this post we want to describe how we can use resource to customize the item look. The first example will describe how we can create rounded corners for each item inside a listview. In the second example we will show how we can alternate the background color.

ListView with rounded corner

Let’s suppose we want to create rounded corner for each item. How can we do this?…We need to create some drawable resources and apply them to each item. As you already know we have to create a custom adapter to implement this behaviour. In this post we don’t want to spend too much words about adapters because we described them here and here.

As we said the first thing we need is a drawable resource. As you may already know this is powerful feature of Android because it permits us to create geometrical figure in XML style. We have to specify some information to create this figure:

  • border size and color
  • background color (in our case a solid color)
  • corners

We need to create a file XML under the res/drawable directory. Let’s call this file rounded_corners.xml. This file contains a shape definition. A shape is a geometrical figure that is described by other tags:

  • stroke – a stroke line for the shape (witdh, color, dashWidth and dashGap)
  • solid – solid colour that fills the shape
  • corners – radius and so o

So the rounded_corners.xml look like:

01  <?xml version="1.0" encoding="utf-8"?>
02  <shape xmlns:android="http://schemas.android.com/apk/res/android" >
03  
04      <solid android:color="#00FF00"/>
05  
06      <corners android:radius="5dp" />
07  
08      <padding android:left="3dp" android:top="3dp" android:right="3dp" android:bottom="3dp" />
09  
10      <stroke android:width="3dp" android:color="#00CC00"/>
11  </shape>

Once we have create our shape we need to apply it to the items. To do it we have to create another XML file that describe how we apply this shape. In this case we use the XML tag selector to specify when and how to apply the shape. To specify when to apply the shape we use the status. We specify to apply this shape when:

  • status = enable
  • status = selected
  • status = pressed

So our file ( listview_selector.xml) looks like:

1   <?xml version="1.0" encoding="utf-8"?>
2   <selector xmlns:android="http://schemas.android.com/apk/res/android" >
3       <item android:drawable="@drawable/rounded_corner" android:state_enabled="true"/>
4   
5       <item android:drawable="@drawable/rounded_corner" android:state_pressed="true"/>
6   
7       <item android:drawable="@drawable/rounded_corner" android:state_focused="true"/>
8   
9   </selector>

Now we have defined our resource, we simply need to specify to apply it in our adapter in this way:

01  public View getView(int position, View convertView, ViewGroup parent) {
02      View v = convertView;
03  
04      PlanetHolder holder = new PlanetHolder();
05  
06      // First let's verify the convertView is not null
07      if (convertView == null) {
08          // This a new view we inflate the new layout
09          LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
10          v = inflater.inflate(R.layout.row_layout, null);
11          // Now we can fill the layout with the right values
12          TextView tv = (TextView) v.findViewById(R.id.name);
13  
14          holder.planetNameView = tv;
15  
16          v.setTag(holder);
17  
18          v.setBackgroundResource(R.drawable.rounded_corner);
19      }
20      else
21          holder = (PlanetHolder) v.getTag();
22  
23      Planet p = planetList.get(position);
24      holder.planetNameView.setText(p.getName());
25  
26      return v;
27  }

If we run the app we have:

kake.JPG

ListView with alternate color

As we describe above, if we want to change how each row look like inside the ListView we have simply change the resource and we can customize its look.

For example we can suppose we want to alternate the row color. In this case we need to create two drawable resource one for each background, like that:

even_row.xml

1   <?xml version="1.0" encoding="utf-8"?>
2   <shape xmlns:android="http://schemas.android.com/apk/res/android" >
3   
4       <solid android:color="#A0A0A0"/>
5   
6       <padding android:left="3dp" android:top="3dp" android:right="3dp" android:bottom="3dp" />
7   
8       <stroke android:width="1dp" android:color="#00CC00"/>
9   </shape>

odd_row.xml

1   <?xml version="1.0" encoding="utf-8"?>
2   <shape xmlns:android="http://schemas.android.com/apk/res/android" >
3   
4       <solid android:color="#F0F0F0"/>
5   
6       <padding android:left="3dp" android:top="3dp" android:right="3dp" android:bottom="3dp" />
7   
8       <stroke android:width="1dp" android:color="#00CC00"/>
9   </shape>

We need moreover two selectors that uses the drawable resources, like that

listviewselectoreven.xml

1   <?xml version="1.0" encoding="utf-8"?>
2   <selector xmlns:android="http://schemas.android.com/apk/res/android" >
3       <item android:drawable="@drawable/even_row" android:state_enabled="true"/>
4   
5       <item android:drawable="@drawable/even_row" android:state_pressed="true"/>
6   
7       <item android:drawable="@drawable/even_row" android:state_focused="true"/>
8   
9   </selector>

listviewselectorodd.xml

1   <?xml version="1.0" encoding="utf-8"?>
2   <selector xmlns:android="http://schemas.android.com/apk/res/android" >
3       <item android:drawable="@drawable/odd_row" android:state_enabled="true"/>
4   
5       <item android:drawable="@drawable/odd_row" android:state_pressed="true"/>
6   
7       <item android:drawable="@drawable/odd_row" android:state_focused="true"/>
8   
9   </selector>

And finally we apply them inside our custom adapter:

01  public View getView(int position, View convertView, ViewGroup parent) {
02      View v = convertView;
03  
04      PlanetHolder holder = new PlanetHolder();
05  
06      // First let's verify the convertView is not null
07      if (convertView == null) {
08          // This a new view we inflate the new layout
09          LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
10          v = inflater.inflate(R.layout.row_layout, null);
11          // Now we can fill the layout with the right values
12          TextView tv = (TextView) v.findViewById(R.id.name);
13  
14          holder.planetNameView = tv;
15  
16          v.setTag(holder);
17  
18          if ( position % 2 == 0)
19            v.setBackgroundResource(R.drawable.listview_selector_even);
20          else
21              v.setBackgroundResource(R.drawable.listview_selector_odd);
22      }
23      else
24          holder = (PlanetHolder) v.getTag();
25  
26      Planet p = planetList.get(position);
27      holder.planetNameView.setText(p.getName());
28  
29      return v;
30  }

Running the app we have:

katie.JPG

Source code @ github

Reference PAGE TOP

http://www.javacodegeeks.com/2013/06/android-listview-background-row-style-rounded-corner-alternate-color.html

0 Comments Leave a comment

Please login in order to leave a comment.

Newest first
!

Sign-in to your Chupamobile Account.

The Easiest way to Launch your next App or Game.

Join Chupamobile and get instant access to thousands of ready made App and Game Templates.

Creating an account means you’re okay with Chupamobile’s Terms of Service and Privacy Policy.