Inflation is a Good Thing

Before diving into the topic of creating fancy lists in Android, we need to take a short detour into some background material. If you have written activities for Android, you are used to calling setContentView() with the resource ID of some XML layout you specified. Let’s take a peek at how that is probably implemented: via a technique called “view inflation”.

In this case, “inflation” means the act of converting an XML layout specification into the actual tree of View objects the XML represents. This is undoubtedly a tedious bit of code: take an element, create an instance of the specified View class, walk the attributes, convert those into property setter calls, iterate over all child elements, lather, rinse, repeat.

The good news is that the fine folk on the Android team wrapped all that up into a class called ViewInflate that we can use ourselves. When it comes to fancy lists, for example, we will want to inflate Views for each row shown in the list, so we can use the convenient shorthand of the XML layout to describe what the rows are supposed to look like.

For example, here is your typical do-nothing activity, except re-written to use ViewInflate instead of supplying the resource ID to setContentView():

[sourcecode language=’java’]
public class InflaterDemo extends Activity {
public void onCreate(Bundle icicle) {

setContentView(getViewInflate().inflate(R.layout.main, null, null));

// setContentView(R.layout.main);

Your activity can get an appropriate instance of ViewInflate via getViewInflate(). Then, to convert a resource ID into a View tree, just call inflate() with the resource ID in question. That gives you your View; in this case, we pass the View to setContentView(), as if we had instantiated the View manually.

In our next episodes, we will see how to create fancy rows for the ListView widget, both using manual construction and using view inflation.

Note: Select outbound links may include affiliate tracking codes. Revenue generated from any potential purchases is used to fund AndroidGuys. Read our policy.



  1. I was reading the book from Apress looking for some visual aid about inflation and I almost write a rant as comment. Luckily enough, the footer says it all and I’m aware about the goot relationship between The Busy Coder’s Guite to Android and Beginning Android 4. Hahaha.

    Cheers and it’s been a good reading, but I’d add more visual aid. =)

  2. involves understanding small nuances of how to…

    generate right kind of traffic and increase the chances of the right person to click on your website, browse through it and act as per the purpose of the site.technically, content writing is mostly guided by certain set of basic principles…

Comments are closed.